STEP Import via API, but affect the prefix/suffix

rcolon9E4ZX
Advocate

STEP Import via API, but affect the prefix/suffix

rcolon9E4ZX
Advocate
Advocate

Hello,

 

We import a lot of purchased component models. I am writing a macro to select the ZIP file, extract to our purchased repository in the workspace, and open the extracted STEP file. When I run the macro, I do not see the Open/Import dialog that I am used to when opening a STEP with the GUI. Instead, the dialog is skipped and the result is the assembly opened/imported with what appears to be default options.

 

Is there a way that I can force this Open/Import dialog to show, like DisplayAlerts? I would also be okay with programmatically setting the Prefix. I tried OpenWithOptions, but I do not see anything useful. Looking through the help on the STEP translator, I did not see any parameters for the NameValueMap that control the prefix or suffix.

 

I have included a picture of the dialog that I am talking about, as well as the code that opens the STEP. Any help with this would be much appreciated.

 

Open/Import Dialog from opening STEP with the GUI.Open/Import Dialog from opening STEP with the GUI.

 

'The path for the STEP file
Dim strSTEP As String
strSTEP = strTarget & strFileNameNoExt
    
'Open the STEP file in Inventor
ThisApplication.Documents.Open strSTEP

 

Kind regards,

Rafael

Mechanical Design Engineer

INV2022.3PRO, WIN10X64

 

0 Likes
Reply
Accepted solutions (2)
597 Views
5 Replies
Replies (5)

A.Acheson
Mentor
Mentor
Accepted solution

Hi @rcolon9E4ZX 

 

That won't appear with the API method as your bypassing the UI functions.

 

Try using control definitions from the command Manager from this article here. This will replicate you clicking file open.

 

'Open an existing document

AppFileOpenCmd    

   

 

   ' Get the CommandManager object.
    Dim oCommandMgr As CommandManager
    Set oCommandMgr = ThisApplication.CommandManager

    ' Get control definition for the line command.
    Dim oControlDef As ControlDefinition
    Set oControlDef = oCommandMgr.ControlDefinitions.Item("AppFileOpenCmd") 
    ' Execute the command.
    Call oControlDef.Execute

 

 

The alternative to the command Manger is controlling import through translator addin. This is more complicated but you do get to cut out manual work of the options are consistently selected each time. Example in the API help sample for .stl here although. Stp is not listed as a sample it can be created via code. I can supply a sample if you need. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes

Michael.Navara
Advisor
Advisor
Accepted solution

You can use STEP Translator for this. Maybe the information is missing in documentation, but it is possible.

Here is the minimal sample

Sub Main()

    Dim stepFile As String = "C:\Path\To\StepFile.stp"

    Dim stepTranslatorId = "{90AF7F40-0C01-11D5-8E83-0010B541CD80}"
    Dim stepTranslator As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById(stepTranslatorId)

    Dim sourceData As DataMedium = ThisApplication.TransientObjects.CreateDataMedium()
    sourceData.FileName = stepFile
    Dim context As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
    context.Type = IOMechanismEnum.kFileBrowseIOMechanism

    Dim target As New Object ' This will be a Document

    Dim options As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
    If stepTranslator.HasOpenOptions(sourceData, context, options) Then
        options.Value("AddFilenamePrefix") = True
        options.Value("FilenamePrefix") = "Prefix_"
    End If

    stepTranslator.Open(sourceData, context, options, target)

    Dim targetDoc as Document = target
    targetDoc.Views.Add()
End Sub

 

All options for STEP translator obtained from default options

-- SaveCopyAsOptions --
IncludeSketches             True
ApplicationProtocolType      3 
Author        
Organization  
Authorization 
Description   
export_fit_tolerance         0,001 
ExportUCS     True

-- OpenOptions --
EnableSaveComponentDuringLoad             False
SaveLocationIndex            2 
ComponentDestFolder         C:\Users\michael.navara\Documents\Inventor\
AssemDestFolder             C:\Users\michael.navara\Documents\Inventor\
SaveAssemSeperateFolder     False
AddFilenamePrefix           False
FilenamePrefix              
AddFilenameSuffix           False
FilenameSuffix              
EmbedInDocument             True
SaveToDisk    False
ImportSolid   True
ImportSurface True
ImportWire    True
ImportPoint   True
ImportMeshes  True
ImportValidationProperties  False
ImportGraphicalPMI          True
CreateIFO     False
ImportAASP    False
ImportAASPIndex              0 
CreateSurfIndex              1 
GroupName     
GroupNameIndex               0 
ExplodeMSB2Assm             False
CEGroupLevel   1 
CEPrefixCk    False
CEPrefixString              Prefix
ImportUnit     0 
CheckDuringLoad             False
AutoStitchAndPromote        True
AdvanceHealing              False
EdgeSplitAndMergeDisabled   False
FaceSplitAndMergeDisabled   False
AssociativeImport           False
Selective Import            False
ImportUCS     False
SaveInSubFolder             True
PathOption    -1 
UserPath      

 

 

rcolon9E4ZX
Advocate
Advocate

@A.Acheson ,

 

Thank you for the response. I forgot about the CommandManager. This will be useful to allow users to enter a custom prefix.

 

Regards,

0 Likes

rcolon9E4ZX
Advocate
Advocate

@Michael.Navara ,

 

Excellent response. Very thorough. And how do you like that, it works. Those elements must be missing from the help document. I have posted the sub below.

 

When I click save, the child files all have the prefix in the name. However, the prefix has not made it into the names in the Model Browser nodes. I know this can be initialized to the filename by clicking on the node to rename, and clearing the box.

 

Would this work the same way if I traverse the assembly and set each oOcc.Name to vbNullString?

 

OpenSTEP code:

Sub OpenSTEP(strSTEP As String, strPath As String, ByVal strPrefix As String)

Dim oSTEPTranslator As TranslatorAddIn
Dim oContext As TranslationContext
Dim oOptions As NameValueMap
Dim oDataMedium As DataMedium
Dim oTarget As Object
Dim oDoc As Document

Dim i As Integer

For i = 1 To ThisApplication.ApplicationAddIns.Count
    If ThisApplication.ApplicationAddIns.Item(i).ClassIdString = "{90AF7F40-0C01-11D5-8E83-0010B541CD80}" Then
        Set oSTEPTranslator = ThisApplication.ApplicationAddIns.Item(i)
        Exit For
    End If
Next

If oSTEPTranslator Is Nothing Then
    MsgBox ("Could not access STEP translator.")
    Exit Sub
End If

Set oContext = ThisApplication.TransientObjects.CreateTranslationContext

oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oOptions.Value("SaveComponentDuringLoad") = False
oOptions.Value("SaveLocationIndex") = 1
oOptions.Value("ComponentDestFolder") = strPath
oOptions.Value("SaveAssemSeperateFolder") = False
oOptions.Value("AssemDestFolder") = strPath
oOptions.Value("AddFilenamePrefix") = True
oOptions.Value("FilenamePrefix") = strPrefix


Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

oDataMedium.FileName = strSTEP

If oSTEPTranslator.HasOpenOptions(oDataMedium, oContext, oOptions) Then
    oOptions.Value("EmbedInDocument") = False

    Call oSTEPTranslator.Open(oDataMedium, oContext, oOptions, oTarget)

    Set oDoc = oTarget
    Call oDoc.Views.Add
    
    If oDoc.DocumentType = kAssemblyDocumentObject Then
        Set oDocFromSTEP = oDoc 'Global used by other module
    End If
End If
End Sub

 

Many thanks and kind regards,

 

Rafael

0 Likes

rcolon9E4ZX
Advocate
Advocate

@Michael.Navara ,

 

Yeah, storing vbNullString into oOcc.Name does initialize the filename into the browser node name. Code below.

 

Sub GroundAllComponents(oAsmDoc As AssemblyDocument)
    Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences)
End Sub

Sub TraverseAssembly(oOccs As ComponentOccurrences)
    Dim oOcc As ComponentOccurrence
    For Each oOcc In oOccs
        On Error Resume Next
        oOcc.Grounded = True
        oOcc.Name = vbNullString
        On Error GoTo -1
        If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
            Call TraverseAssembly(oOcc.SubOccurrences)
        End If
    Next
End Sub

 

Regards,

0 Likes