-
Re: I need a macro that will delete all unshown configurations in an assembly
Adam Hoffman May 6, 2015 2:12 PM (in response to Barry Setzer)This should get you started:
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swConFig As SldWorks.Configuration
Dim sConFigName As String
Dim vConFigNames As Variant
Dim i As Integer
Dim bRet As BooleanSub main()
Set swApp = _
Application.SldWorksIf swApp.GetDocumentCount() = 0 Then Exit Sub
Set swModel = swApp.ActiveDoc
If swModel.GetType() = swDocumentTypes_e.swDocDRAWING Then Exit Sub
If swModel.GetConfigurationCount() = 1 Then Exit Sub
Set swConFig = swModel.GetActiveConfiguration
vConFigNames = swModel.GetConfigurationNames
For i = 0 To UBound(vConFigNames)
sConFigName = vConFigNames(i)
If Not sConFigName = swConFig.Name Then
bRet = swModel.DeleteConfiguration2(sConFigName)
End If
Next iswModel.ForceRebuild3 (False)
MsgBox ("Deletion Macro Complete")
End Sub
-
Re: I need a macro that will delete all unshown configurations in an assembly
Keith Rice May 6, 2015 4:32 PM (in response to Barry Setzer)To have it work at the assembly level, you could combine Adam's macro with the macro in my Macro Library called "Run code on every part in an assembly". (Requires a free membership)
Keith
-
Re: I need a macro that will delete all unshown configurations in an assembly
Barry Setzer May 7, 2015 11:12 AM (in response to Keith Rice)Keith, I posted this as a new question, but since I'm having trouble getting the "all components" part to work, I figured that you are likely the most qualified to help me out..........By the way, nice macros!!!
So I have 3 operations here. The first deletes suppressed components from my assembly. Then it calls the "allcomponents" sub. Allcomponents calls the "delcon" Sub.........this deletes all configurations from the assembly but not the individual components.......what am I missing?
Option Explicit
Dim swApp As SldWorks.SldWorks
Public iCountDelete As Integer
Public sNameDelete As StringSub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swConf As SldWorks.Configuration
Dim swRootComp As SldWorks.Component2
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swConf = swModel.GetActiveConfiguration
Set swRootComp = swConf.GetRootComponentTraverseComponent swRootComp, swModel, 0
MsgBox ("Barry Has Cleaned Your File. You're Welcome!")
End SubSub TraverseComponent(swComp As SldWorks.Component2, RootModel As SldWorks.ModelDoc2, ByVal iLev As Integer)
Dim vChildComp As Variant
Dim swChildComp As SldWorks.Component2
Dim swCompConfig As SldWorks.Configuration
Dim i As Long
Dim iRueck As Double
Dim oModel, oModComp As ModelDoc2
Dim iSel As Object
Dim b As Boolean
Dim o As Object
Dim iLevel As Integer
iLevel = iLev + 1 ' This variable is meant to show the sub-level the macro currently works in
Debug.Print iLevel & "|" & swComp.NamevChildComp = swComp.GetChildren
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
If swChildComp.IsSuppressed Then ' Identified a suppressed part - Aim is to kill this one, no matter if it is assembly or part
Debug.Print "Suppr.: " & swChildComp.Name
swComp.Select4 False, Nothing, False
RootModel.EditAssembly
b = swChildComp.Select4(False, iSel, False)
b = RootModel.Extension.DeleteSelection2(1)
iCountDelete = iCountDelete + 1
RootModel.ClearSelection2 (True)
RootModel.EditAssemblyIf b = False Then i = swChildComp.SetSuppression2(0)
End IfTraverseComponent swChildComp, RootModel, iLevel
Next iCall AllComponents
End Sub
Public Sub AllComponents()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim i As Integer
Dim vComps As Variant
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
vComps = swAssy.GetComponents(True)
If IsEmpty(vComps) Then Exit Sub
For i = 0 To UBound(vComps)
Set swComp = vComps(i)
Set swCompModel = swComp.GetModelDoc2
Call Delcon
Next iEnd Sub
Public Sub Delcon()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swConFig As SldWorks.Configuration
Dim sConFigName As String
Dim vConFigNames As Variant
Dim j As Integer
Dim bRet As Boolean
Set swApp = _
Application.SldWorks
If swApp.GetDocumentCount() = 0 Then Exit Sub
Set swModel = swApp.ActiveDoc
If swModel.GetType() = swDocumentTypes_e.swDocDRAWING Then Exit Sub
If swModel.GetConfigurationCount() = 1 Then Exit Sub
Set swConFig = swModel.GetActiveConfiguration
vConFigNames = swModel.GetConfigurationNames
For j = 0 To UBound(vConFigNames)
sConFigName = vConFigNames(j)
If Not sConFigName = swConFig.Name Then
bRet = swModel.DeleteConfiguration2(sConFigName)
End If
Next j
swModel.ForceRebuild3 (False)
End Sub
-
-
Re: I need a macro that will delete all unshown configurations in an assembly
Adam Hoffman May 10, 2015 12:59 PM (in response to Barry Setzer)Barry,
Try this macro here. The problem that took some extra time working around was: What if you had parts in your assembly that came from the same part file? The same file in the assembly, multiple times, but was using different configurations from that file. So after cleaning the configs in the top level, delete all suppressed components, then build a list of all used configuration names in this assembly. After that you can traverse the assembly deleting unused configs keeping all the referenced configs. (And if it's a sheet metal part, the flat-pattern for that config) I'm still a beginner, and I usually do all my programming in VB.Net, not Solidworks VBA. So this macro is not a pretty one, but I ran it through a couple of tests and it seems to work on my end. I haven't done extensive testing with it, so there still might be certain situations that need tweaking. Let me know if you have to make changes to it, I'd be curious about the mistakes I made. That way I might not make them in the future. Hope this helps.
-
Clean Configurations.swp.zip 20.8 KB
-
Re: I need a macro that will delete all unshown configurations in an assembly
Barry Setzer May 12, 2015 8:13 AM (in response to Adam Hoffman)Beautiful Adam! My definition of a "pretty" macro is one that WORKS!!! So yours is very pretty indeed!!!!! I appreciate the assist because this one was making me crazed.........and there were no signs of me figuring out the issue on my own.
-
Re: I need a macro that will delete all unshown configurations in an assembly
Adam Hoffman May 12, 2015 1:31 PM (in response to Barry Setzer)No problem. Glad to help.
-
Re: I need a macro that will delete all unshown configurations in an assembly
Cuinn Herrick Jul 23, 2015 9:02 PM (in response to Adam Hoffman)This is great! Thank you for your effort!
I wonder how hard it would be to delete suppressed features also in the assembly and parts.
The only issue I can think of is if an unsuppressed feature referencing the sketch in a suppressed feature?
-
-
-
Re: I need a macro that will delete all unshown configurations in an assembly
Radomir Holusa Feb 19, 2018 7:59 AM (in response to Adam Hoffman)Thank you for Macro, it was very helpful.
However, is it possible to also aply this for deleting sheet tabs in drawing? Each unshown configuration has also its own drawing (sheet tab). If I use your macro, I will delete these unshown configurations in model, hovewer in drawing sheet tabs of deleted configurations will still be there.
Thank you.
-
-
Re: I need a macro that will delete all unshown configurations in an assembly
Cuinn Herrick Oct 17, 2016 10:29 PM (in response to Barry Setzer)For anyone interested, I have added a function to avoid cleaning design library parts or components.
-
CleanConfigurations.zip 23.9 KB
-
Re: I need a macro that will delete all unshown configurations in an assembly
Nick Tzallas Jan 17, 2018 10:25 PM (in response to Cuinn Herrick)Ok so this is a great help! Thank you Adam Hoffman Cuinn Herrick
I do not mean to resurrect a relatively old thread but I am in a bit of a bind
Seeing as my macro fluency/comprehension is non-existent, is there any link/literature that could teach me how I could modify that particular Macro so I could simply select a folder with hundreds of files and just run it on that?
i.e. it would open each file one by one, perform "clean inactive configs", save and then close file. move onto the next one and so on.
Having to open each file and apply the macro manually will take a toll on me rather fast seeing as it would need to be applied to 1000's of parts (building fasteners library, sans toolbox)
There might be an obvious answer and/or link here, but I am ever so slightly clueless
-
Re: I need a macro that will delete all unshown configurations in an assembly
Dennis Dohogne Jan 17, 2018 10:32 PM (in response to Nick Tzallas)Nick Tzallas wrote:
Ok so this is a great help! Thank you Adam Hoffman Cuinn Herrick
I do not mean to resurrect a relatively old thread but I am in a bit of a bind
Seeing as my macro fluency/comprehension is non-existent, is there any link/literature that could teach me how I could modify that particular Macro so I could simply select a folder with hundreds of files and just run it on that?
i.e. it would open each file one by one, perform "clean inactive configs", save and then close file. move onto the next one and so on.
Having to open each file and apply the macro manually will take a toll on me rather fast seeing as it would need to be applied to 1000's of parts (building fasteners library, sans toolbox)
There might be an obvious answer and/or link here, but I am ever so slightly clueless
Nick,
This sounds like it is right up the alley of #TASK #TASK
If it doesn't do exactly as you want then follow the information here Setting up a custom task with macro
-
Re: I need a macro that will delete all unshown configurations in an assembly
Nick Tzallas Jan 17, 2018 11:02 PM (in response to Dennis Dohogne)Thank you Dennis Dohogne
Yup I have #task installed and use it for a number of things, it is a godsend for Macro-illiterate folk like me.
I have seen the custom task video several times, but seeing as it shows a relatively simple example (and one made from scratch), I wouldn't know where to begin for this particular case.
Do I record a macro of me opening and running the "clean configurations" macro (sort of inception style) and then modify that in #task?
Or edit the existing macro in #task? but then what in the world do I type?
Apologies, just hinking out loud as it were.
I need to find a novice #task video of some kind that explains the logic. or jack into the matrix and download "uber macro writing skills programme"
Thank you
-
-
Re: I need a macro that will delete all unshown configurations in an assembly
Nick Tzallas Jan 18, 2018 1:18 AM (in response to Deepak Gupta)Thank you Deepak Gupta
I think I just posted at the same time.
Yes the #task library is awesome Just didn't have the exact thing I was looking for
I'm just starting to stumble into all the #task / Macro stuff. As you can see in my previous post, I'm very easily impressed with it
-
-
Re: I need a macro that will delete all unshown configurations in an assembly
Nick Tzallas Jan 18, 2018 1:15 AM (in response to Nick Tzallas)Yaaaaas!!
Ok so I know this is simple baby steps for the Macro-literate folk here, but I just had my mind blown, so here goes,
- Just convert the .swp file into a.sts file so it runs properly in #task (as per video linked in previous post) [that said i guess you could simply load the .swp file directly into #task too)
- Load it into #task
- in the "item scope" point to the folder you want (instead of a single file)
- make sure write access and apply to all configs is selected in the tasks options
- Run job
- Hey presto!
The only thing I did was remove the Line of code from the macro that had the Message pop up at then end of a successful run of said macro, as it was holding up the process in #task and would do it for each config removal. Once I deleted the line all went smoothly.
I realise this is all ridiculously obvious but I hadn't put 2 and 2 together. Didn't think that the opening of the folder, saving and closing could be taken care of by #task itself rather than having to code those steps into the macro too.
Thank you Dennis Dohogne for the suggestion! ( I just hadn't made the link/click in my mind)
and thank you Adam Hoffman and Cuinn Herrick for the Macro once again!
-
-
-
-
-
Re: I need a macro that will delete all unshown configurations in an assembly
Dennis Dohogne Oct 18, 2016 10:15 AM (in response to Barry Setzer)For your desire to delete unused configurations in a part file you can use Purge introduced in SWX2016. For the assembly you can bookmark this and vote when the time comes: Have Purge function first do Where Used and extend it to assemblies
-
Re: I need a macro that will delete all unshown configurations in an assembly
Cuinn Herrick Oct 18, 2016 8:58 PM (in response to Dennis Dohogne)Dennis Dohogne are you confusing this with purge unused features? Also your link appears to require authorized access?
-
Re: I need a macro that will delete all unshown configurations in an assembly
Dennis Dohogne Oct 19, 2016 9:49 AM (in response to Cuinn Herrick)Cuinn Herrick, I was just suggesting that in some cases the Purge function might do just what is being asked for. Like anything else, there are cases when one tool is better than another.
The link is to a posted "idea" in the SOLIDWORKS World 2017 Top Ten List . If you cannot access that list then you should contact Matthew Lorono.
-
-