I need a macro that will add the following equations to a part. The macro must also work for assemblies.
"Surface area [cm²]"= "SW-SurfaceArea" / 100
"Volume [cm³]"= "SW-Volume" / 1000
"Mass [kg]"= "SW-Mass" / 1000
Any help with this is highly appreciated.
SolidworksApi macros
Why you can't keep them as variable in the templates itself?
I have the equations in our new templates, but I'm looking for an quick and easy way to add these equations to old parts and assemblies.
This code works on current open file but can be changed to work on multiple file i.e. batch operation.
<div id="_mcePaste">Dim swApp As Object</div><div id="_mcePaste">Dim Part As Object</div><div id="_mcePaste">Dim boolstatus As Boolean</div><div id="_mcePaste">Dim longstatus As Long, longwarnings As Long</div><div id="_mcePaste"> </div><div id="_mcePaste">Sub main()</div><div id="_mcePaste"> </div><div id="_mcePaste">Set swApp = Application.SldWorks</div><div id="_mcePaste"> </div><div id="_mcePaste">Set Part = swApp.ActiveDoc</div><div id="_mcePaste">boolstatus = Part.Extension.SelectByID2("Equations", "EQNFOLDER", 0, 0, 0, False, 0, Nothing, 0)</div><div id="_mcePaste"> </div><div id="_mcePaste">Dim swEquationMgr As Object</div><div id="_mcePaste">Set swEquationMgr = Part.GetEquationMgr()</div><div id="_mcePaste">swEquationMgr.Add -1, """Surface area [cm²]""= ""SW-SurfaceArea""/100"</div><div id="_mcePaste">swEquationMgr.Add -1, """Volume [cm³]""= ""SW-Volume""/100"</div><div id="_mcePaste">swEquationMgr.Add -1, """Mass [kg]""= ""SW-Mass""/100"</div><div id="_mcePaste">boolstatus = Part.ForceRebuild()</div><div id="_mcePaste">Part.Save</div><div id="_mcePaste"> </div><div id="_mcePaste">End Sub</div>Thanks a lot, Deepak!
Your code is just what I need. Batch operation is not relevant this time, because all our files are in a PLM system.
Something a bit strange happens if I run the macro several times. I then get some suppressed equations, and two active Mass...-equations which seems to be identical . But I don't think this will be a big problem.
I too have the same behavior and I have no clue for the reasons.
Check and Delete the Existing Equation and Add the new One
The macro can be edited to delete any existing similar equation before adding a new one.
Try these
<div id="_mcePaste">Dim swApp As Object</div><div id="_mcePaste">Dim Part As Object</div><div id="_mcePaste">Dim boolstatus As Boolean</div><div id="_mcePaste">Dim swEquationMgr As Object</div><div id="_mcePaste">Dim longstatus As Long, longwarnings As Long</div><div id="_mcePaste">Sub main()</div><div id="_mcePaste"> </div><div id="_mcePaste">Set swApp = Application.SldWorks</div><div id="_mcePaste">Set Part = swApp.ActiveDoc</div><div id="_mcePaste"> </div><div id="_mcePaste">'Selects the below mentioned Equations</div><div id="_mcePaste"> </div><div id="_mcePaste">boolstatus = Part.Extension.SelectByID2("Surface area [cm²]", "EQNFOLDER", 0, 0, 0, False, 0, Nothing, 0)</div><div id="_mcePaste">boolstatus = Part.Extension.SelectByID2("Volume [cm³]", "EQNFOLDER", 0, 0, 0, False, 0, Nothing, 0)</div><div id="_mcePaste">boolstatus = Part.Extension.SelectByID2("Mass [kg]", "EQNFOLDER", 0, 0, 0, False, 0, Nothing, 0)</div><div id="_mcePaste"> </div><div id="_mcePaste">'Deletes the above selected Equations</div><div id="_mcePaste"> </div><div id="_mcePaste">Part.DeleteAllRelations</div><div id="_mcePaste"> </div><div id="_mcePaste">'Adds the below mentioned Equations</div><div id="_mcePaste"> </div><div id="_mcePaste">Set swEquationMgr = Part.GetEquationMgr()</div><div id="_mcePaste">swEquationMgr.Add -1, """Surface area [cm²]""= ""SW-SurfaceArea""/100"</div><div id="_mcePaste">swEquationMgr.Add -1, """Volume [cm³]""= ""SW-Volume""/100"</div><div id="_mcePaste">swEquationMgr.Add -1, """Mass [kg]""= ""SW-Mass""/100"</div><div id="_mcePaste"> </div><div id="_mcePaste">'Rebuild and Save the file.</div><div id="_mcePaste">boolstatus = Part.ForceRebuild()</div><div id="_mcePaste">Part.Save</div><div id="_mcePaste"> </div><div id="_mcePaste">End Sub</div>Deepak,
I think the last 2 SelectByID2 calls should have append flag as true or else the earlier items shall be deselected.
The modified macro should be
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim swEquationMgr As Object
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
'Selects the below mentioned Equations
boolstatus = Part.Extension.SelectByID2("Surface area [cm²]", "EQNFOLDER", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Volume [cm³]", "EQNFOLDER", 0, 0, 0, true, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Mass [kg]", "EQNFOLDER", 0, 0, 0, true, 0, Nothing, 0)
'Deletes the above selected Equations
Part.DeleteAllRelations
'Adds the below mentioned Equations
Set swEquationMgr = Part.GetEquationMgr()
swEquationMgr.Add -1, """Surface area [cm²]""= ""SW-SurfaceArea""/100"
swEquationMgr.Add -1, """Volume [cm³]""= ""SW-Volume""/100"
swEquationMgr.Add -1, """Mass [kg]""= ""SW-Mass""/100"
'Rebuild and Save the file.
boolstatus = Part.ForceRebuild()
Part.Save
End Sub
Hope it helps!!!
Rajat Jain
Agree with you but with false too, the macro works as required as it must be taking this into consideration
With False as append, "if an item is Already selected, the current selection list remains the same"
oops..
Didn't know that...
thats a gud learning..
thanks
Rajat Jain
Hello Deepak
I would like to add the following equation with your macro but can'd figure out how to break down the code. I tried to break it down into strings but it keeps coming up with errors.
I can copy past the equation manually but need to find a way to insert it via macro.
"GAUGE"= part.extension.custompropertymanager("").set("Stock"&"No",trim(right( "" & format(val("N"),"00") &" GA / " & format(val("SSZ"),"0.000"),val("C"))))
thanks,
Elmar
Deepak,
This macro deletes all equations that may exist in the part. How do I delete only the equations that will be replaced by the macro?
Randy
You should know the index (position) of that equation. You can get this value/number by looking at the ordered view in equation manager.
The index starts from 0 i.e. equation at number 1 position is at 0 index. Change the following codes to delete the required equation.
Deepak,
How do i modify this macro to get the surface area of a selected face only?
To get surface area of selected face:
Hello Deepak,
This macro is also useful for me...
I have change eqaution as i need but i need an aditional thing in this that my derived value is in only three decimal.
is it possible in that way...?
Also i need to conver this as a batch macro..
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim swEquationMgr As Object
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
'Adds the below mentioned Equations
Set swEquationMgr = Part.GetEquationMgr()
swEquationMgr.Add -1, """Area""= ""SW-Volume""/ ""Thickness""/1000000"
'Rebuild and Save the file.
boolstatus = Part.ForceRebuild()
Part.Save
End Sub
Use ROUND function. Check for e.g. macro by Dan here: https://forum.solidworks.com/message/459714#459714
Thanks deepak..got it