Page 1 of 1

Custom Save Macro

Posted: Sat Dec 04, 2021 11:38 am
by Dwight
All

I am looking for a macro to Save files with a list of adjustments made to the file prior to saving such as . . .
  • Remove any roll backs.
  • Set many Show/Hide Types to "show", including sketches and reference geometry.
  • Hide all reference items in the FeatureManager.
  • Hide all sketches in the FeatureManager.
  • Set the view to "Isometric".
  • Set the Background to "None".
  • Autosize reference geometry.
  • Image Quality Deviation set to "4".
  • Unit decimals to 8 places.
  • Dimensions to 3 places.
It might also check some conditions and error-out if not right:
  • No configuration named "Default".
  • Units not "Millimeters".
Let me know if you have a macro that does some of that. I might be able to handle some macro customization, but I am not much for writing them from scratch.

Thanks

Dwight

Re: Custom Save Macro

Posted: Tue Dec 21, 2021 4:52 pm
by Dwight
I am trying to build this up by recording macros and assembling the results. Probably I am creating a monster, but it is behaving so far.

I have a section that hides all the reference features in the FeatureManager, which seems to work (see the first chunk below). I would also like to use "Part.BlankSketch" (last line below) to hide all the sketches, but I don't know how to select all the sketches first. Any ideas?

Thanks

Dwight



'hide features
Set swUtil = swApp.GetAddInObject("Utilities.UtilitiesApp")
Set swUtilPsl = swUtil.PowerSelect
longstatus = swUtilPsl.Init()
longstatus = swUtilPsl.SetSelectEntitiesTypes(8)
longstatus = swUtilPsl.SetShowHiddenEntities(False)
Dim featTypeArr1(0 To 4) As Long
featTypeArr1(0) = 401
featTypeArr1(1) = 402
featTypeArr1(2) = 403
featTypeArr1(3) = 404
featTypeArr1(4) = 405
longstatus = swUtilPsl.SetFeatureTypeFilter((featTypeArr1))
varEntCount = swUtilPsl.RunPowerSelect(0, longstatus)
longstatus = swUtilPsl.SelectResults()
longstatus = swUtilPsl.Close()
Part.BlankRefGeom

Part.BlankSketch

Re: Custom Save Macro

Posted: Tue Dec 21, 2021 7:27 pm
by Emra
One possibility is to go through all the features, and add the ones with type "Sketch" to a selection list. The help documentation has a pretty good example of how to do this here that you could more or less copy/paste into your macro: https://help.solidworks.com/2020/englis ... ple_VB.htm

Hope this helps!

Re: Custom Save Macro

Posted: Wed Dec 22, 2021 12:51 am
by gupta9665

Re: Custom Save Macro

Posted: Wed Dec 22, 2021 12:54 pm
by Dwight
Emra and Deepak

Thanks, those are helpful. I find I must learn more about the basics of API to make progress, so that is what I am doing. I find Peter Brinkhuis at CADBooster and Luke Malpass at Anglefire both helpful. I do prefer to read and am not much for watching videos.

Thanks

Dwight

Re: Custom Save Macro

Posted: Fri Dec 24, 2021 8:25 am
by Dwight
I have this macro mostly working. There's a few spots I need to work on, but it's coming along.

Can anyone say why my swPlane.Autosize value doesn't change when I run this code? It runs the line okay, but the value doesn't change.

If featType = "RefPlane" Then
Set swPlane = swFeat.GetDefinition
If swPlane.Type2 = 11 Then
swPlane.AutoSize = True
swPlane.UpdatePlane = True
DidSelect = swFeat.ModifyDefinition(swPlane, swModel, Nothing)
End If
End If

I copied the whole macro below.

Thanks

Dwight




' ******************************************************************************
' C:\Users\10056185\AppData\Local\Temp\swx3112\Macro1.swb - macro recorded on 12/20/21 by 10056185
' ******************************************************************************

Dim swApp As SldWorks.SldWorks
Dim swModel As IModelDoc2
Dim Transfer As String

Sub SetSettings(Transfer As String)

Dim boolstatus As Boolean
Dim Scene As SldWorks.SWScene
Dim swConfig As SldWorks.Configuration

Set swConfig = swModel.GetActiveConfiguration
Set Scene = swConfig.GetScene
Scene.BackgroundType = swSceneBackgroundType_e.swBackgroundType_None

boolstatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swDetailingLinearDimPrecision, swUserPreferenceOption_e.swDetailingDimension, 4)
boolstatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swDetailingAltLinearDimPrecision, swUserPreferenceOption_e.swDetailingDimension, 4)
boolstatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitSystem, 0, swUnitSystem_e.swUnitSystem_MMGS)
boolstatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swImageQualityShaded, 2, 2)
boolstatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swImageQualityWireframeValue, 0, 14)
boolstatus = swModel.FeatureManager.EditRollback(swMoveRollbackBarTo_e.swMoveRollbackBarToEnd, "")
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayAllAnnotations, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayAxes, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayCenterOfMassSymbol, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayCoordSystems, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayDatumCoordSystems, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayCurves, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayOrigins, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayPlanes, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayPartingLines, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayReferencePoints2, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplaySketches, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayDecals, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swHideShowSketchDimensions, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swViewSketchRelations, True)
boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swViewDisplayHideAllTypes, False)

End Sub

Sub HideFeatures(Transfer As String)

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Dim swFeat As SldWorks.Feature
Set swFeat = swModel.FirstFeature
Dim featType As String
Dim Append As Boolean
Dim Mark As Integer
Dim DidSelect As Boolean
Dim swPlane As IRefPlaneFeatureData

While Not swFeat Is Nothing

featName = swFeat.Name
featType = swFeat.GetTypeName
DidSelect = swFeat.Select2(Append, Mark)

If swModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then
DidSelect = swModel.EditUnsuppress2()
End If

If featType = "RefPlane" Or _
featType = "RefAxis" Or _
featType = "CoordSys" Then
swModel.BlankRefGeom
End If

If featType = "RefPlane" Then
Set swPlane = swFeat.GetDefinition
If swPlane.Type2 = 11 Then
swPlane.AutoSize = True
swPlane.UpdatePlane = True
DidSelect = swFeat.ModifyDefinition(swPlane, swModel, Nothing)
End If
End If

If featType = "ProfileFeature" Or _
featType = "OriginProfileFeature" Or _
featType = "3DProfileFeature" Then
swModel.BlankSketch
End If

Set swFeat = swFeat.GetNextFeature

Wend

Set swFeat = swModel.FirstFeature
DidSelect = swFeat.Select2(Append, Mark)

End Sub

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

If swModel Is Nothing Then
MsgBox "Please open a model first"
End
End If

If swModel.GetType <> swDocumentTypes_e.swDocPART And _
swModel.GetType <> swDocumentTypes_e.swDocASSEMBLY Then
MsgBox "Please open a part model or assembly model first"
End
End If

If swModel.GetConfigurationByName("Default") Is Nothing Then
Dim swConfig As SldWorks.Configuration
Set swConfig = swModel.AddConfiguration3("Default", "", "", swConfigurationOptions2_e.swConfigOption_UseDescriptionInBOM)
MsgBox "A Default configuration was created. Please delete other configurations if they are not needed."
End If
swModel.ShowConfiguration2 ("Default")

SetSettings Transfer
HideFeatures Transfer

swModel.ShowNamedView2 "*Isometric", 7
swModel.ViewZoomtofit2

' Save
Dim swErrors As Long
Dim swWarnings As Long
boolstatus = swModel.Save3(1, swErrors, swWarnings)

Re: Custom Save Macro

Posted: Sun Dec 26, 2021 1:45 am
by gupta9665
The help file may explain in finding out why autosize doesn't work https://help.solidworks.com/2022/Englis ... Redirect=1

As per help file, Autosize doesn't work with constraint-based reference planes. Fo e.g. plane create by selecting vertices or via coincident to a point.

Re: Custom Save Macro

Posted: Tue Dec 28, 2021 12:30 pm
by Dwight
gupta9665 wrote: Sun Dec 26, 2021 1:45 am As per help file, Autosize doesn't work with constraint-based reference planes. Fo e.g. plane create by selecting vertices or via coincident to a point.
Thanks, Deepak. I saw the table before but did not see the key at the bottom. I assumed that "X" meant it applied and "?" meant it might apply. Silly me. So really that AutoSize is useless.

I found that I can use RunCommand and get swCommands_Auto_Size to autosize a plane that I preselect in FeatureManager (see example below), but not on a plane selected by the macro, where the macro is traversing through all the features. Is there a way to make a plane summoned by the macro using swFeat.Select2 the same as a plane I preselected in the FeatureManager?

Thanks

Dwight


Dim swApp As SldWorks.SldWorks
Dim swModel As IModelDoc2
Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

Dim swModelDocExt As SldWorks.ModelDocExtension
Set swModelDocExt = swModel.Extension
swModelDocExt.RunCommand swCommands_Auto_Size, ""

End Sub

Re: Custom Save Macro

Posted: Tue Dec 28, 2021 12:34 pm
by Dwight
Deepak

Wait - I found it! It doesn't work if the plane is hidden, but it does work if it is showing.

Dwight

Re: Custom Save Macro

Posted: Sat Jan 01, 2022 10:37 pm
by gupta9665
Dwight, try the attached macro (some codes I have used from your macro directly and some from the below example)

https://help.solidworks.com/2022/englis ... ple_VB.htm

Re: Custom Save Macro

Posted: Mon Jan 10, 2022 3:20 pm
by Dwight
Deepak

Thanks for your reply. The macro I wrote did not traverse assembly components, so sometime I might be interested in adding that feature. I'm not that good at doing this so it will take a very deep dive for me to figure out what your code does.

I did finish my custom model save macro and deployed it in our group. Seems to be working fine. Now I am on to other things. Probably it will be a long time before I write another macro and I will have to figure it out all over again.

Thanks for your help.

Dwight