Hi all,
I'm writing a small VB6.0 app that saves multibody part to a numberof STL's along with a file that's used as command/preprocessorbatch file for our casting analysis software (MAGMAsoft).
App checks body folders (as defined by designer), selects body bybody in certain order (as required by MAGMA modeling principle) andspits them out as STL, while appending certain info to an ASCIIfile.
Problem is in STL triangulation time. At complex models(hydrodynamic shapes, many bodies), SolidWorks triangulates ALLbodies first, then asks do I want to save all bodies or only aselected one (it does this for each body again and again). While Ican save all these bodies in separate files in one hit, it be muchbetter option for me to be able to do it via code.
So, my questions are:
1. Is there a way to make SW triangulate and save *only* selectedbody, without triangulating "kitchen and the sink" first?
2. Short of workable answer to Q1, what is the best way to delete abody using VB6.0?
Cheers and thanks,
MladenSolidworksApi macros
I'm writing a small VB6.0 app that saves multibody part to a numberof STL's along with a file that's used as command/preprocessorbatch file for our casting analysis software (MAGMAsoft).
App checks body folders (as defined by designer), selects body bybody in certain order (as required by MAGMA modeling principle) andspits them out as STL, while appending certain info to an ASCIIfile.
Problem is in STL triangulation time. At complex models(hydrodynamic shapes, many bodies), SolidWorks triangulates ALLbodies first, then asks do I want to save all bodies or only aselected one (it does this for each body again and again). While Ican save all these bodies in separate files in one hit, it be muchbetter option for me to be able to do it via code.
So, my questions are:
1. Is there a way to make SW triangulate and save *only* selectedbody, without triangulating "kitchen and the sink" first?
2. Short of workable answer to Q1, what is the best way to delete abody using VB6.0?
Cheers and thanks,
MladenSolidworksApi macros

Option Explicit
Private Sub Command1_Click()
Dim swApp As SldWorks.SldWorks
Dim swModel1 As SldWorks.ModelDoc2
Dim swPart1 As SldWorks.PartDoc
Dim swBody1 As SldWorks.Body2
Dim vswBody1 As Variant
Dim vBodyResArr1 As Variant
Dim bret As Boolean
Dim i As Integer
Set swApp = GetObject(, "SldWorks.application")
Set swModel1 = swApp.ActiveDoc
Set swPart1 = swModel1
vBodyResArr1 = swPart1.GetBodies2(swSolidBody, True)
For Each vswBody1 In vBodyResArr1
swModel1.ClearSelection
Set swBody1 = vswBody1
swBody1.Select False, -1
Set swPart1 = swApp.ActiveDoc
bret = swPart1.SaveToFile2("d:\x" & i & ".stl", 0, 0, 0):Debug.Assert bret
Set swPart1 = swApp.ActiveDoc
swApp.CloseDoc swPart1.GetTitle
i = i + 1
Next vswBody1
End Sub
Ivana, yes, in essence, that's what I currently use. But I got toit entirely accidentally, thanks to forgetting to remove ".stl" inswpart.SaveToFile2 syntax (copy'n'paste, you know the drill...).
My problem and question posted was/is with swPart.SaveAs4 commandas an "official" way of dealing with conversions. In a year or fewtime, if I need to maintain the code, I'll likely forget thatswpart.SaveToFile2 was used as a trick (API manual says it's forpart saving as in*.sldprt !!!). So, I anticipate I'd search the manual andwander why on Earth did I use a command that's not even mentioningSTL..... It's about a clean code.
Anyway, I've sent an enhancement request, so let's see what comesup as swPart.SaveAs4 is currently unusable for my type of issue.
Cheers,
Mladen
But, speaking about it, why would Ineed to hide/unhide bodies? I want it working without"trickery" or trial and error, so when my designers want to dotheir bit of job at hand, it works without any preconditions... andI don't need to write any "manuals" ;-).
As said above, it's about clean code that's easy to maintain intime to come, not about tricks and preconditions. Sure, I'd do itif that's the only way, but it's not.
Option Explicit
Private Sub SaveBodiesToSTL()
Dim swApp As SldWorks.SldWorks
Dim swModel1 As SldWorks.ModelDoc2
Dim swPart1 As SldWorks.PartDoc
Dim swBody1 As SldWorks.Body2
Dim vswBody1 As Variant
Dim vBodyResArr1 As Variant
Dim bret As Boolean
Dim i As Integer
Dim Save2Name As String
Set swApp = GetObject(, "SldWorks.application")
Set swModel1 = swApp.ActiveDoc
Set swPart1 = swModel1
vBodyResArr1 = swPart1.GetBodies2(swSolidBody, True)
On Error GoTo GRAPHICSRESTORE
swModel1.ActiveView.EnableGraphicsUpdate = False
For Each vswBody1 In vBodyResArr1
swModel1.ClearSelection
Set swBody1 = vswBody1
swBody1.Select False, -1 'select the body of interest
swModel1.Extension.RunCommand swCommands_InvertSelection, ""'invert selection to select all other bodies
swModel1.HideSolidBody 'hide the selected bodies
Save2Name = Left(swModel1.GetPathName, Len(swModel1.GetPathName) -7) & " - " & swBody1.Name & ".stl"
bret = swModel1.SaveAs4(Save2Name, 0, swSaveAsOptions_Silent +swSaveAsOptions_Copy, Empty, Empty)
swBody1.Select False, -1 're-select the body of interest
swModel1.Extension.RunCommand swCommands_InvertSelection, ""'select all the hidden bodies
swModel1.ShowSolidBody 'show all the selected hidden bodies
i = i + 1
Next vswBody1
GRAPHICSRESTORE:
swModel1.ActiveView.EnableGraphicsUpdate = True
MsgBox "Finished"
End Sub
Option Explicit
Private Sub FastSaveBodiesToSTL()
Dim swApp As SldWorks.SldWorks
Dim swModel1 As SldWorks.ModelDoc2
Dim swBody1 As SldWorks.Body2
Dim swPart1 As SldWorks.PartDoc
Dim vBodyResArr1 As Variant
Dim bret As Boolean
Dim i As Integer
Dim Save2Name As String
Set swApp = GetObject(, "SldWorks.application")
Set swModel1 = swApp.ActiveDoc
Set swPart1 = swModel1
vBodyResArr1 = swPart1.GetBodies2(swSolidBody, True) 'array ofvisible bodies
On Error GoTo GRAPHICSRESTORE
swModel1.ActiveView.EnableGraphicsUpdate = False 'disable graphicsfor performance
swModel1.ClearSelection2 True
'hide all the visible bodies
For i = 0 To UBound(vBodyResArr1)
Set swBody1 = vBodyResArr1(i)
swBody1.HideBody True
Next i
'show and save bodies to .stl one by one
For i = 0 To UBound(vBodyResArr1)
Set swBody1 = vBodyResArr1(i)
swBody1.HideBody False
Save2Name = Left(swModel1.GetPathName, Len(swModel1.GetPathName) -7) & " - " & swBody1.Name & ".stl"
bret = swModel1.SaveAs4(Save2Name, 0, swSaveAsOptions_Silent +swSaveAsOptions_Copy, Empty, Empty)
swBody1.HideBody True
Next i
're-show all the bodies hidden earlier
For i = 0 To UBound(vBodyResArr1)
Set swBody1 = vBodyResArr1(i)
swBody1.HideBody False
Next i
GRAPHICSRESTORE:
swModel1.ActiveView.EnableGraphicsUpdate = True
If Err.Number <> 0 Then
MsgBox "Finished with errors"
Else
MsgBox "Finished"
End If
End Sub
I get your point: it CAN be done if hide/unhide used, but I'd sayyou didn't get mine. Perhaps I should have been clearer: I meantwhy do I need to hide bodies in order to export visible one(s)? Whynot just select and export selected? My take is SW needs to enablethis option.
Not everybody knows nor it should know about effect of hide/unhideat export (I didn't), hence calling it a "trick"...
...Not in the manual - it's a "trick" (or experience).
As one of the colleagues at Ansys forum said it, just paraphrasedfor SolidWorks:
"Overcoming SolidWorks deficiencies since 1996"
Cheers,
Mladen