Save an STP file of each part where a specific parameter exists

Save an STP file of each part where a specific parameter exists

ngowans
Contributor Contributor
431 Views
2 Replies
Message 1 of 3

Save an STP file of each part where a specific parameter exists

ngowans
Contributor
Contributor

I need to write a script which goes through an assembly and all of the subassemblies, checks parts for a specific parameter matching a specific value (IsProfile = True) and if the parameter matches, open the part and save a copy as an STP file using the part reference as the file name.

 

I have ripped off and attempted to modify the script below, but it wants to open every part in the model. in my case there are 1900 (200+ unique instances) parts, once the part is open it will then try match the value and if it's able it should try to export the part, however since there are so many parts and the vast majority of them don't contain the required parameter (or do but the parameter is not matching) I don't want to have inventor spend two days opening 1900 components and only exporting 20 of them.  

 

I'm fairly new to iLogic and I'm happy setting and looping through parts but this is a little beyond my skill level. 

 

so for clarity, I want to,

Go through each component, 

If it has a specific parameter (IsProfile = True)

Open the part and save a copy as an STP using the part number as filename

Close the part and move on to the next

Else

Skip the part and move on to the next

 

 

'check that the active document is an assembly file
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
    MessageBox.Show("This Rule " & iLogicVb.RuleName & " only works on Assembly Files.", "WRONG DOCUMENT TYPE", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Return
End If

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAsmName As String = ThisDoc.FileName(False) 'without extension


'get user input
Dim RUsure = MessageBox.Show(
"This will create a STEP file for all components." _
& vbLf & " " _
& vbLf & "Are you sure you want to create STEP Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic - Batch Output STEPs ", MessageBoxButtons.YesNo)
If RUsure = vbNo Then
    Return
Else
End If

'- - - - - - - - - - - - -STEP setup - - - - - - - - - - - -
Dim oPath = ThisDoc.Path
'get STEP target folder path
' original => oFolder = oPath & "\" & oAsmName & " STEP Files"
Dim oFolder = oPath & "\ExportedComponents"
'Check for the step folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If


'- - - - - - - - - - - - -Assembly - - - - - - - - - - - -
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName & (".stp"), True)

'- - - - - - - - - - - - -Components - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
'work the referenced models
For Each oRefDoc As Document In oRefDocs
	
	Dim oCurFile As Document = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
    Dim oCurFileName = oCurFile.FullFileName
    Dim ShortName = IO.Path.GetFileNameWithoutExtension(oCurFileName)

    Dim oPropSets As PropertySets = oCurFile.PropertySets
    Dim oPropSet As PropertySet = oPropSets.Item("Design Tracking Properties")
    Dim oPartNumiProp As [Property] = oPropSet.Item("Part Number")

If Parameter.Param("IsProfile") = True Then
    Try
        oCurFile.SaveAs(oFolder & "\" & oPartNumiProp.Value & (".stp"), True)
    Catch
        MessageBox.Show("Error processing " & oCurFileName, "ilogic")
    End Try
    oCurFile.Close()
Else 
	oCurFile.Close()
End If

Next
'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")

 

Any help would be massively appreciated.

 

0 Likes
Accepted solutions (1)
432 Views
2 Replies
Replies (2)
Message 2 of 3

A.Acheson
Mentor
Mentor
Accepted solution

 

You might be able to remove these lines as they are opening all the references files regardless of the filter your using. These files are open all ready in the context of the assembly. Replace oCurfile with oRefDoc. If this doesn't work you can just open them after you filter for the documents via the  parameter.

 

Dim oCurFile As Document = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
    Dim oCurFileName = oCurFile.FullFileName
    Dim ShortName = IO.Path.GetFileNameWithoutExtension(oCurFileName)

 

For the parameter here 

Is the link to the api help for this and the sample. 

 

 

Dim oParam As [Parameter]
 Try
oParam = oRefDoc.ComponentDefinition.Parameters.Item("IsProfile")
If oParam.Value = True Then

Logger.Info(oRefDoc.FullFileName)

Try oRefDoc.SaveAs(oFolder & "\" & oPartNumiProp.Value & (".stp"), True) Catch MessageBox.Show("Error processing " & oCurFileName, "ilogic") End Try Else End If

Catch
End Try

Also it is best turn off the save as and open section of code untill you start returning the right file names. You can use the logger.info to check those are correct. No danger of opening lots of files and waisting your time.

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

ngowans
Contributor
Contributor

With a little squeezing this works perfectly and now I think I understand the relationships between referenced components and parameters a little more. Much appreciated.