.STL export of multibody part

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
Answers 9
IK
IK 2009-01-19 Best Answer
is your code something like this?

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

JB
JB 2009-01-19
I would recommend traversing the bodies twice. The first timethrough do nothing but hide all the bodies. The next time through,show each body, export, and re-hide. I guess you could traverse athird time and re-show all the bodies. This should run prettyquickly. To further improve performance, you might want to trysetting ModelView::EnableGraphicsUpdate to false. If you do this,though, youmust remember to set it back to true, and youmust have an error handling routine to set it back to true.Otherwise if your macro ends due to an error, there is no way toreset EnableGraphicsUpdate through the user interface.
MM
MM 2009-01-20
Thanks for your replies.

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

JB
JB 2009-01-20
Why is it unusable for your type issue? Why can't you hidebodies then save?
MM
MM 2009-01-21
I haven't tried your suggestion because I got working codealready.
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.
JB
JB 2009-01-21
It's not a "trick" or a precondition. You do it all in code.Easy as the proverbial pie. You asked about deleting bodies. Thatwould certainly be a bad way to do it. Hiding bodies will "hide"them from export. They won't get triangulated.
JB
JB 2009-01-21
This code is a slight modification of Ivana's code. It usesSaveAs4 to silently save all bodies to .stl in the same folder asthe original document. The stl file has the name of the originaldocument followed by the name of the body from the design tree. Itincorporates enabling/disabling of graphics updating. Nopreconditions, no "tricks". The code is easy to follow later, evenif you don't use any comments.

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


JB
JB 2009-01-21
Here is a more elegant, easy to maintain method for doing thesame as above. And it runs about 10 times faster. Rather than usingthe RunCommand function (which basically mimics user actions and isvery slow), it operates directly on the bodies.

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
MM
MM 2009-01-22
Josh, thanks for the effort.

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