iLogic - Create rule for exporting surfacebodies to stl (in mm and high quality)

iLogic - Create rule for exporting surfacebodies to stl (in mm and high quality)

gregory.noe
Observer Observer
526 Views
1 Reply
Message 1 of 2

iLogic - Create rule for exporting surfacebodies to stl (in mm and high quality)

gregory.noe
Observer
Observer

Hi everybody,

 

I would like to export surfacebodies to stl files in mm and high quality. I've already try one but unity is still centimeters. Can someone help me please ? 

 


'Dim début As Date
début = now()


Dim Doc As PartDocument
Dim Corps As SurfaceBody
Dim CorpsToExport As SurfaceBody
Dim Export As DataIO
Dim Etat_Initial As New Collection
Dim Nom As String
Dim chemin As String
Dim oOptions As NameValueMap

Doc = ThisApplication.ActiveDocument
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
chemin=ThisDoc.Path+"\STL "+ThisDoc.FileName(False)
'Mémorisation de l'état de visibilité
For Each Corps In Doc.ComponentDefinition.SurfaceBodies
Etat_Initial.Add(Corps.Visible,Corps.Name)
Next
'On masque tous les corps
For Each Corps In Doc.ComponentDefinition.SurfaceBodies
Corps.Visible =False
Next
'On rétablit un à un chaque corps solide et on exporte en stl
For Each CorpsToExport In Doc.ComponentDefinition.SurfaceBodies
If Etat_initial(CorpsToExport.Name) = True Then
CorpsToExport.Visible=True
'Nom = CorpsToExport.Name
Nom = chemin + "\" + CorpsToExport.Name + ".stl"
oOptions.Value("ExportUnits") = 5 '5 = millimètres
ThisDoc.Document.SaveAs(Nom, True)
CorpsToExport.Visible=False
End If
Next

'on rétablit l'état initial

For Each Corps In Doc.ComponentDefinition.SurfaceBodies
Corps.Visible = Etat_Initial.Item(Corps.Name)
Next
fin = now()

 

 

0 Likes
527 Views
1 Reply
Reply (1)
Message 2 of 2

WCrihfield
Mentor
Mentor

Hi @gregory.noe.  I don't think you can export just a single SurfaceBody object from a PartDocument out as a STL file.  You have to export the whole document.  So, if you only want the resulting STL file to contain the one SurfaceBody, you may have to create a temporary copy of the PartDocument with just that one specific SurfaceBody remaining within it, then save it to disk.  Then export that temporary PartDocument out as a STL file.  Then delete that temporary PartDocument.  I don't think you can just turn the visibility of other SurfaceBodies off, because those bodies are still there in the PartDocument and still have Volume & Mass.  Those other SurfaceBodies must be removed, one way or another, before exporting the document.  You may be able to suppress the feature(s) that created each of the other SurfaceBodies, if that is possible for you.

Here is an example of exporting a Document as an STL file, with the options you specified:

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		MsgBox("A Part Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oPDoc As PartDocument = ThisDoc.Document
	oExportedFileName = System.IO.Path.ChangeExtension(oPDoc.FullFileName, ".stl")
	ExportToSTL(oPDoc, oExportedFileName)
End Sub

Sub ExportToSTL(oDoc As Document, oNewFullFileName As String)
	If IsNothing(oDoc) Or oNewFullFileName = "" Then Exit Sub
	Dim oSTL As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById( _
	"{533E9A98-FC3B-11D4-8E7E-0010B541CD80}")
	If IsNothing(oSTL) Then Exit Sub
		
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium
	oDataMedium.FileName = oNewFullFileName
	If oSTL.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
		oOptions.Value("ExportUnits") = 5 'millimeters
		oOptions.Value("Resolution") = 0 '0 = High
		oOptions.Value("AllowMoveMeshNode") = False
		oOptions.Value("SurfaceDeviation") = 5 '~0.005%
		oOptions.Value("NormalDeviation") = 1000 '10%
		oOptions.Value("MaxEdgeLength") = 100000 '100%
		oOptions.Value("AspectRatio") = 2150 'top end when on High
		oOptions.Value("ExportFileStructure") = 0 'only used when using ASCII
		oOptions.Value("OutputFileType") = 0 '0 = Binary (altertative is ASCII)
		oOptions.Value("ExportColor") = True
		Try
			oSTL.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
		Catch oEx As Exception
			MsgBox("SaveCopyAs Failed." & vbCrLf & _
			oEx.Message & vbCrLf & oEx.StackTrace, vbCritical,"")
		End Try
	End If
End Sub

The options shown within that rule coincide with the below images:

When the options are set like this:

WCrihfield_0-1653055172440.png

Then the values of those code options come out like this:

WCrihfield_1-1653055210787.png

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes