iLogic rule to generate step file for model states with unique part numbers

iLogic rule to generate step file for model states with unique part numbers

b.mccarthy
Collaborator Collaborator
513 Views
9 Replies
Message 1 of 10

iLogic rule to generate step file for model states with unique part numbers

b.mccarthy
Collaborator
Collaborator

Hello.

 

I have a basic rule which will generate step files for parts and assemblies:

 

' Generates a Step file using Application Protocol 242
' Get the STEP translator Add-In.
Dim oSTEPTranslator As TranslatorAddIn
oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap

If oSTEPTranslator.HasSaveCopyAsOptions(ThisDoc.Document, oContext, oOptions) Then
' Set application protocol.
' 2 = AP 203 - Configuration Controlled Design
' 3 = AP 214 - Automotive Design
' 4 = AP 214IS - ?
' 5 = AP 242 - Managed model-based 3D engineering
oOptions.Value("ApplicationProtocolType") = 3
' Other options...
'oOptions.Value("Author") = ""
'oOptions.Value("Authorization") = ""
'oOptions.Value("Description") = ""
'oOptions.Value("Organization") = ""
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oData As DataMedium
oData = ThisApplication.TransientObjects.CreateDataMedium
oData.FileName = ThisDoc.PathAndFileName(False) & ".stp"
oSTEPTranslator.SaveCopyAs(ThisDoc.Document, oContext, oOptions, oData)
End If

'MessageBox.Show("STEP GENERATED", "")

 

Can this be modified to cycle through each model state in a file, and generate a step file for any state that has a unique part number? (Not a coder...)

 

If this has already been done, please post a link.

 

TIA

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

Ivan_Sinicyn
Advocate
Advocate

Hi,

' Get the STEP translator Add-In
Dim oSTEPTranslator As TranslatorAddIn
oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap

If oSTEPTranslator.HasSaveCopyAsOptions(ThisDoc.Document, oContext, oOptions) Then
    ' Set application protocol to AP 242
    oOptions.Value("ApplicationProtocolType") = 5
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
    
    ' Get the document and its model states
    Dim oDoc As Document
    oDoc = ThisDoc.Document
    Dim oModelStates As ModelStates
    oModelStates = oDoc.ComponentDefinition.ModelStates
    
    ' Save the initial active model state
    Dim initialModelState As ModelState
    initialModelState = oDoc.ComponentDefinition.ModelStates.ActiveModelState
    
    ' Collection to track unique PartNumbers
    Dim uniquePartNumbers As New Collection
    
    ' Iterate through all model states
    For Each oModelState As ModelState In oModelStates
        ' Activate the current model state
        oModelState.Activate
        
        ' Get PartNumber from properties
        Dim partNumber As String
        partNumber = iProperties.Value("Project", "Part Number")
        
        ' Skip if PartNumber is already processed
        On Error Resume Next
        uniquePartNumbers.Add (partNumber, partNumber)
        If Err.Number <> 0 Then
            Err.Clear
            GoTo NextModelState
        End If
        On Error GoTo 0
        
        ' Clean filename from invalid characters
        partNumber = Replace(partNumber, "\", "_")
        partNumber = Replace(partNumber, "/", "_")
        partNumber = Replace(partNumber, ":", "_")
        partNumber = Replace(partNumber, "*", "_")
        partNumber = Replace(partNumber, "?", "_")
        partNumber = Replace(partNumber, """", "_")
        partNumber = Replace(partNumber, "<", "_")
        partNumber = Replace(partNumber, ">", "_")
        partNumber = Replace(partNumber, "|", "_")
        
        ' Construct the file path
        Dim filePath As String
        filePath = ThisDoc.Path & "\" & partNumber & ".stp"
        
        ' Create data medium for saving
        Dim oData As DataMedium
        oData = ThisApplication.TransientObjects.CreateDataMedium
        oData.FileName = filePath
        
        ' Save the STEP file
        oSTEPTranslator.SaveCopyAs(oDoc, oContext, oOptions, oData)
        
NextModelState:
    Next
    
    ' Restore the initial model state
    initialModelState.Activate
End If

 
But it is not clear what to do with model states that have the same partnumber. I made automatic skipping of such states. Only the first unique one is exported.

INV 2025.3
Message 3 of 10

b.mccarthy
Collaborator
Collaborator

@Ivan_Sinicyn 

 

Very nice! Thank you for this!

 

I tested it on a simple assembly with 2 plates and a bolted connection, and have some ?'s...

  • One of the model states had a component suppressed. When I first ran the rule, the suppressed component briefly disappeared in the window, and then re-appeared, although on subsequent runs this did not occur. I have some large, complex assemblies I intend on using the rule for, so waiting for each state to rebuild and display in the window is not optimal
  • You say "...I made automatic skipping of such states. Only the first unique one is exported..." which is perfect, as long as it is the Primary model state. Is that the case?
  • If I make changes to any model state, (and I will) does your code overwrite all existing step files when run?

Thank you!

0 Likes
Message 4 of 10

Ivan_Sinicyn
Advocate
Advocate

@b.mccarthy 
The problem with Model States is that only the active model state is written to the file, and all other states are stored only as tabular data for optimization. Therefore, each model state must be activated in the component.

As for the uniqueness of PartNumber. If you have several model states, for example,
[Primary] PartNumber: AAA-111
MS1 PartNumber: BBB-111
MS2 PartNumber: AAA-111 (It will be skipped, because this PartNumber has already been exported earlier).

| If I make changes to any model state, (and I will) does your code overwrite all existing step files when run?
Files are overwritten each time the code is activated

If you want to use the code from the assembly, another code must be written, which will export components with active model states according to the list of component entries. In this case, you do not need to activate the model states of the component separately. But if your top-level assembly also contains model states, you will need to activate the model states one by one for this assembly.

INV 2025.3
0 Likes
Message 5 of 10

b.mccarthy
Collaborator
Collaborator

@Ivan_Sinicyn 

 

Thank you for the explanation, as I am trying to understand your code (from a non-coder perspective...). The rule will begin generating the step files starting with the Primary state, regardless of the active state. Correct?

 

 

 

0 Likes
Message 6 of 10

Ivan_Sinicyn
Advocate
Advocate
Accepted solution

Activation occurs in the order of states in the ModelStates collection, starting with oModelStates.Item(1) That is, it starts with Primary and goes down the list. At the end of processing it restores the active state of the model at the moment of code activation

INV 2025.3
Message 7 of 10

b.mccarthy
Collaborator
Collaborator

@Ivan_Sinicyn

 

This will work great. The only drawback is that the various model states display in the window as the rule cycles. This was occurring because the rule was triggering on "Before Save Document". After I moved it to "Before Vault Check-in" the model states did not display, and the step files generated successfully.

 

Thank you for your help!!

0 Likes
Message 8 of 10

b.mccarthy
Collaborator
Collaborator

@Ivan_Sinicyn 

 

Hello, Ivan.

 

I am receiving this occasional error:

 

2025-06-24 1130.png

 

Any idea why?

 

Thank you.

0 Likes
Message 9 of 10

Ivan_Sinicyn
Advocate
Advocate

It is very difficult to understand the cause as the error code is generic and there is no exact understanding of what caused it. As a rule, there is no direct access to the file.

I've tried several variants of simulating problems, but never got an error like yours.

INV 2025.3
0 Likes
Message 10 of 10

b.mccarthy
Collaborator
Collaborator

The error appears randomly, and only on assemblies with 3,000 parts, or more.

 

The step files are generating correctly, so I will ignore it when it appears.

 

Thank you.

0 Likes