Batch export model state STL

Batch export model state STL

inventor4578
Advocate Advocate
1,025 Views
9 Replies
Message 1 of 10

Batch export model state STL

inventor4578
Advocate
Advocate

Hi,

 

I try to make a bulk export of my .ipt part into .stl files with 1 .stl by Model State

But the problem is for 20 Model state, i get 4 "good" file, and 16 "1 Ko" files (bad file).

 

Sub Main
   
    Dim oDoc As PartDocument
    oDoc = ThisDoc.Document 
    
    Dim oModelState As ModelState
    
    For Each oModelState In oDoc.ComponentDefinition.ModelStates
        Logger.Info(oModelState.Name)
        If oModelState.Name <> "Master" Then
            oModelState.Activate
            ExportSTL(oModelState.Name)
        End If
    Next
    
End Sub

Sub ExportSTL(ByVal oState As String)
    ' Get the STL translator Add-In.
   Dim oSTLTranslator As TranslatorAddIn
    oSTLTranslator = ThisApplication.ApplicationAddIns.ItemById("{81CA7D27-2DBE-4058-8188-9136F85FC859}")
    
    Dim oContext As TranslationContext
    oContext = ThisApplication.TransientObjects.CreateTranslationContext
    
    Dim oOptions As NameValueMap
    oOptions = ThisApplication.TransientObjects.CreateNameValueMap
    
    oPath = ThisDoc.Path
    oFolder = oPath & "\MODEL_STATES_STL"
    
    'Check for the STL folder and create it if it does not exist
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
    
    If oSTLTranslator.HasSaveCopyAsOptions(ThisDoc.Document, oContext, oOptions) Then
  
        
        oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
        Dim oData As DataMedium
        oData = ThisApplication.TransientObjects.CreateDataMedium
        oData.FileName = oFolder & "\" & ThisDoc.FileName(False) & "-" & oState & ".stl"
        
        oSTLTranslator.SaveCopyAs(ThisDoc.Document, oContext, oOptions, oData)
    End If
End Sub

.

 

0 Likes
Accepted solutions (1)
1,026 Views
9 Replies
Replies (9)
Message 2 of 10

Frederick_Law
Mentor
Mentor

What's wrong with the bad file?

0 Likes
Message 3 of 10

inventor4578
Advocate
Advocate

Bad files are empty (1kb)

If i open the file with notepad i just have 1 line : "STLB ATF 10.19.0.1499", that all.

I tryed on another part, it seam to work ? That strange ?

0 Likes
Message 4 of 10

Frederick_Law
Mentor
Mentor

Can you manually export STL on those failed parts?

 

Check and set options for export:

https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-25D24E28-7517-4903-8AEE-123F8C71A89E&v=In...

 

Export as ASCII.  Some slicer has problem reading Binary.

 

Also some slicer can read STEP.

Assuming you're 3D printing them.

Message 5 of 10

WCrihfield
Mentor
Mentor

Hi @inventor4578.  In addition to what @Frederick_Law said about trying manual process and setting some of the options when doing it by code, I would suggest that you avoid using the 'ThisDoc' term within your ExportSTL Sub routine, and consider passing the value of the ModelState.Document property to the ExportSTL Sub routine, instead of just the name of the ModelState.  Then use that supplied Document object, instead of 'ThisDoc.Document' phrase in those two methods within that routine.  Using ThisDoc.Path and ThisDoc.FileName(False) would likely be OK, because it will be the same for all ModelStates because they are all in the same file.  I am guessing that the term ThisDoc may keep referring to the originally active document reference though, instead of the document associated with the currently activated ModelState while you are looping through them and activating them, which would be a problem.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 10

inventor4578
Advocate
Advocate
Hi.
It's working with manual export ASCII, with my STEP macro is working also for all the model states.
0 Likes
Message 7 of 10

BM_Ashraf
Advocate
Advocate
Accepted solution

I think the process is too fast and you need to update the document before you export the .stl files

check this one

Sub Main
   
    Dim oDoc As PartDocument
    oDoc = ThisDoc.Document 
    
    Dim oModelState As ModelState
    
    For Each oModelState In oDoc.ComponentDefinition.ModelStates
        Logger.Info(oModelState.Name)
        If oModelState.Name <> "Master" Then
            oModelState.Activate
			InventorVb.DocumentUpdate()
            ExportSTL(oModelState.Name)
        End If
    Next
    
End Sub

Sub ExportSTL(ByVal oState As String)
    ' Get the STL translator Add-In.
   Dim oSTLTranslator As TranslatorAddIn
    oSTLTranslator = ThisApplication.ApplicationAddIns.ItemById("{81CA7D27-2DBE-4058-8188-9136F85FC859}")
    
    Dim oContext As TranslationContext
    oContext = ThisApplication.TransientObjects.CreateTranslationContext
    
    Dim oOptions As NameValueMap
    oOptions = ThisApplication.TransientObjects.CreateNameValueMap
    
    oPath = ThisDoc.Path
    oFolder = oPath & "\MODEL_STATES_STL"
    
    'Check for the STL folder and create it if it does not exist
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
    
    If oSTLTranslator.HasSaveCopyAsOptions(ThisDoc.Document, oContext, oOptions) Then
  
        
        oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
        Dim oData As DataMedium
        oData = ThisApplication.TransientObjects.CreateDataMedium
        oData.FileName = oFolder & "\" & ThisDoc.FileName(False) & "-" & oState & ".stl"
        
        oSTLTranslator.SaveCopyAs(ThisDoc.Document, oContext, oOptions, oData)
    End If
End Sub


 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.

Blue Mech

Add-ins for Inventor!

Message 8 of 10

inventor4578
Advocate
Advocate

Hi, thank for reply ! I test it

0 Likes
Message 9 of 10

BM_Ashraf
Advocate
Advocate

is it possible to share the test file ?

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.

Blue Mech

Add-ins for Inventor!

0 Likes
Message 10 of 10

inventor4578
Advocate
Advocate
Now it's working, all files are good.
Thank you very much,
Regards,