ilogic: exclude derived parts in export loop assembly components

ilogic: exclude derived parts in export loop assembly components

Anonymous
Not applicable
964 Views
4 Replies
Message 1 of 5

ilogic: exclude derived parts in export loop assembly components

Anonymous
Not applicable

Dear all,

 

This is my first post and i am a total N00b in Visual Basic, but nevertheless i'm writing some stuff in ilogic.

 

I'm writing a rather large routine but part of it is exporting parts within an assembly to IGES and STEP files (in a folder on the server with the correct name,etc...)

 

My code:

'- - - - - - - - - - - - - Component 3D models format export - - - - - - - - - - - 

Dim oIGESpartdata As DataMedium
Dim oSTEPpartdate As DataMedium

Dim oRefParts As DocumentsEnumerator
oRefParts= oAsmDoc.AllReferencedDocuments
Dim oRefPart  As Document

	' Iterate through each part in the assembly
	For Each oRefPart In oRefParts
	iptPathName = Left(oRefPart.FullDocumentName, Len(oRefPart.FullDocumentName) -3) & "ipt"
		Dim oPartDoc As PartDocument
		oPartDoc = ThisApplication.Documents.Open(iptPathName, True)
		oPartName = System.IO.Path.GetFileNameWithoutExtension(oRefPart.FullFileName)
		oRevNumPart = oRefPart.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value
			
			'IGES export subparts
			oIGESpartdata = ThisApplication.TransientObjects.CreateDataMedium
			oIGESpartdata.FileName = oFolder & "\" & oPartName & " V" & oRevNumPart & ".igs"
			oIGESTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oIGESpartdata)
			
			'STEP export subparts
			oSTEPpartdata = ThisApplication.TransientObjects.CreateDataMedium
			oSTEPpartdata.FileName = oFolder & "\" & oPartName & " V" & oRevNumPart & ".stp"
			oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oSTEPContext, oSTEPOptions, oSTEPpartdata)
			
		oPartDoc.Close(True)
	Next

My problem at the moment is that it also does the export for derived parts, which it should not.

At the moment it exports all parts it finds, so both green and red arrows, but i want to exclude the part with the red arrow.

assembly_structure.png

(I'd like to keep the link with the base part, if this is relevant to the solution)

 

Sorry if my code is hectic, as i said i'm new at this.

0 Likes
Accepted solutions (1)
965 Views
4 Replies
Replies (4)
Message 2 of 5

lmc.engineering
Advocate
Advocate
Accepted solution

Hi @Anonymous

 

I believe you'll want something like the below code. I have corrected a few things and cleaned it all up a little too.

 

SyntaxEditor Code Snippet

Sub Main()
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oRefParts As DocumentsEnumerator
oRefParts = oAsmDoc.AllReferencedDocuments
Dim oRefPart As Document
'Define array lists to hold file name values
Dim aExcludedFileList As New ArrayList
Dim aOpenFileList As New ArrayList

Dim oFile As File
Dim oFileDescriptor As FileDescriptor

	' Iterate through each part in the assembly
	For Each oRefPart In oRefParts
		iptPathName = Left(oRefPart.FullDocumentName, Len(oRefPart.FullDocumentName) -3) & "ipt"
		aOpenFileList.Add(iptPathName)
		oFile = oRefPart.File
		'Check for further Referenced files ( Derived Parts)
		If oFile.ReferencedFileDescriptors.Count = 0 Then
		Else
			'If Reference is found, add file name to the excluded list
			For Each oFileDescriptor In oFile.ReferencedFileDescriptors		
				sDerivePart = oFileDescriptor.FullFileName
				aExcludedFileList.Add(sDerivePart)
			Next
		End If
	Next
	
	'Remove any duplicates from excluded list
	For i As Integer = aExcludedFileList.Count - 1 To 0 Step -1
            'Check whether this is the first instance of this item.
            If aExcludedFileList.IndexOf(aExcludedFileList(i)) <> i Then
                'This is not the first instance of this item so remove it.
                aExcludedFileList.RemoveAt(i)
            End If
        Next i
	
	'Iterate through valid occurrence files
	For Each sFile In aOpenFileList
		'Check against excluded files
		For Each sItem In aExcludedFileList
			'If files are not on the excluded list, process them
			If Not sItem = sFile Then
				'Open Part
				oPartDoc = ThisApplication.Documents.Open(sFile, True)
				'Set paths and properties
				sFolder = "YOUR FILE PATH" '***************************************************************************************************
				sPartName = System.IO.Path.GetFileNameWithoutExtension(oPartDoc.FullFileName)
				sRevNumPart = oPartDoc.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value	
				'Process IGES
				ProcessIGES(sFolder, sPartName, sRevNumPart)
				'Process STEP
				ProcessSTEP(sFolder, sPartName, sRevNumPart)
				'Close Document
				oPartDoc.Close(True)
			End If
		Next
	Next
End Sub



Function ProcessIGES(sFolder As String, sPartName As String, sRevNumPart As String)
' Get the IGES translator Add-In.
Dim oIGESTranslator As TranslatorAddIn
oIGESTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F44-0C01-11D5-8E83-0010B541CD80}")
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
If oIGESTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
   ' Set geometry type for wireframe.
   ' 0 = Surfaces, 1 = Solids, 2 = Wireframe
   oOptions.Value("GeometryType") = 1
   ' To set other translator values:
   ' oOptions.Value("SolidFaceType") = n
   ' 0 = NURBS, 1 = Analytic
   ' oOptions.Value("SurfaceType") = n
   ' 0 = 143(Bounded), 1 = 144(Trimmed)
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oData As DataMedium
oData = ThisApplication.TransientObjects.CreateDataMedium
oData.FileName = sFolder & "\" & sPartName & " V" & sRevNumPart & ".igs"
oIGESTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
End If

End Function

Function ProcessSTEP(sFolder As String, sPartName As String, sRevNumPart As String)
	' 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(ThisApplication.ActiveDocument, oContext, oOptions) Then
    ' Set application protocol.
    ' 2 = AP 203 - Configuration Controlled Design
    ' 3 = AP 214 - Automotive Design
    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 = sFolder & "\" & sPartName & " V" & sRevNumPart & ".stp"
oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
End If
End Function

Just as a note for future: when defining local variables we generally will use a small letter in front of the descriptor to indicate what data type it is; i.e. o for Object, s for string, b for boolean etc.. So a File object will become oFile, a file name will become sFileName. It's nothing major if this isn't adhered to, but it really does help with decoding things after not opening the code for a period of time.

 

Hope this helps.

 

Regards

Message 3 of 5

Anonymous
Not applicable

Hello @lmc.engineering,

 

Thank you for your response.

I've implemented your code and it works like a charm !

 

I will keep your 'style tips' in mind when i continue writing my code.

Thank you again Smiley Very Happy

 

0 Likes
Message 4 of 5

ic198
Advocate
Advocate

Hi lmc,

 

I have a similar problem to the OP- I want to open an assembly and print all the drawings associated with all subassemblies and parts in that assembly. I have some code that is working except that it will print drawings associated with derived parts too.

I was going to use your code but reading it through with my limited vb knowledge I think it will only print everything at level 1 in the assembly? i.e. it will exclude parts in subassemblies etc- is that correct?

 

Best regards

0 Likes
Message 5 of 5

Anonymous
Not applicable

Underneath the code we work with now.

It exports all assemblies en parts in the different formats and their drawings underneath, not just the first level.

(It also exports base-parts underneath derived functions)

 

' - - - - - - - - - - Assembly Export - - - - - - - - - - - - - - - - -
' Add required libraries and add-ins,...

AddReference "System.IO.Compression"
AddReference "System.IO.Compression.FileSystem"

' - - - - - - - - - - Main program - - - - - - - - - - - - - - - - - - -
Sub Main

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)

'check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If

'get user input
RUsure = MessageBox.Show ( _
"Er wordt een PDF-file gemaakt voor alle assembly-onderdelen die over een drawing beschikken." _
& vbLf & "Er wordt een IGES-file gemaakt van de assembly." _
& vbLf & "Deze -rule- eist dat alle drawings dezelfde naam en locatie hebben als de componenten." _
& vbLf & "" _
& vbLf & "Ben je zeker dat er PDF-tekeningen van alle assembly onderdelen moeten worden gemaakt?" _
& vbLf & "Dit kan een tijdje duren.", "iLogic  - Batch Output PDFs ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then
Return
Else
End If

'- - - - - - - - - - - - -PDF en IGS setup - - - - - - - - - - - -
oPath = "\\PPLHHBB5\data\11.PRODUCTIE\Drawings\SAMENSTELLINGEN"
'Get the PDF-translator  Add-Inn 
PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
' Get the IGES translator Add-In.
Dim oIGESTranslator As TranslatorAddIn
oIGESTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F44-0C01-11D5-8E83-0010B541CD80}")
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
If oIGESTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
   ' Set geometry type for wireframe.
   ' 0 = Surfaces, 1 = Solids, 2 = Wireframe
   oOptions.Value("GeometryType") = 1
   ' To set other translator values:
   oOptions.Value("SolidFaceType") = 0
   ' 0 = NURBS, 1 = Analytic
   oOptions.Value("SurfaceType") = 0
   ' 0 = 143(Bounded), 1 = 144(Trimmed)
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
End If

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

If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oSTEPContext, oSTEPOptions) Then
    ' Set application protocol.
    ' 2 = AP 203 - Configuration Controlled Design
    ' 3 = AP 214 - Automotive Design
    oSTEPOptions.Value("ApplicationProtocolType") = 3
    ' Other options...
    'oSTEPOptions.Value("Author") = ""
    'oSTEPOptions.Value("Authorization") = ""
    'oOSTEPptions.Value("Description") = ""
    'oSTEPOptions.Value("Organization") = ""
    oSTEPContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
End If


oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 0
oOptions.Value("Vector_Resolution") = 600
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'oOptions.Value("Custom_Begin_Sheet") = 2
'oOptions.Value("Custom_End_Sheet") = 4
End If

'get target folder path
oFolder = oPath & "\"  & oAsmName & " V" & iProperties.Value("Project", "Revision Number")

'Check for the PDF folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)

Else
'-----------------------------------
'MESSAGE BOX 
'query user
question = MessageBox.Show("Deze folder bestaat reeds, wilt u deze opnieuw aanmaken?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
'set condition based on answer 
If question = vbNo Then
 
'gather input from user, uses current parameter as input default
Exit Sub
'------------------------------------


Else
End If 

iLogicVb.UpdateWhenDone = True
    
    
    
End If
'- - - - - - - - - - - - -

'- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document

'work the the drawing files for the referenced models
'this expects that the model has a drawing of the same path and name 
For Each oRefDoc In oRefDocs
idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "idw"
'check to see that the model has a drawing of the same path and name 
If(System.IO.File.Exists(idwPathName)) Then
        Dim oDrawDoc As DrawingDocument
    oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
    oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -4)
    oRevNum = oRefDoc.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value
                

    On Error Resume Next ' if PDF exists and is open or read only, resume next
     'Set the PDF target file name
    oDataMedium.FileName = oFolder & "\" & oFileName & " V" & oRevNum & ".pdf"
    'Write out the PDF
    Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
    'close the file
    oDrawDoc.Close(True)
Else
'If the model has no drawing of the same path and name - do nothing
End If
Next

'- - - - - - - - - - - - - Component 3D models format export - - - - - - - - - - - 

Dim oIGESpartdata As DataMedium
Dim oSTEPpartdate As DataMedium

Dim oRefParts As DocumentsEnumerator
oRefParts= oAsmDoc.AllReferencedDocuments
Dim oRefPart  As Document

    ' Iterate through each part in the assembly
    For Each oRefPart In oRefParts
    iptPathName = Left(oRefPart.FullDocumentName, Len(oRefPart.FullDocumentName) -3) & "ipt"
        Dim oPartDoc As PartDocument
        oPartDoc = ThisApplication.Documents.Open(iptPathName, True)
        oPartName = System.IO.Path.GetFileNameWithoutExtension(oRefPart.FullFileName)
        oRevNumPart = oRefPart.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value
            
            'IGES export subparts
            oIGESpartdata = ThisApplication.TransientObjects.CreateDataMedium
            oIGESpartdata.FileName = oFolder & "\" & oPartName & " V" & oRevNumPart & ".igs"
            oIGESTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oIGESpartdata)
            
            'STEP export subparts
            oSTEPpartdata = ThisApplication.TransientObjects.CreateDataMedium
            oSTEPpartdata.FileName = oFolder & "\" & oPartName & " V" & oRevNumPart & ".stp"
            oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oSTEPContext, oSTEPOptions, oSTEPpartdata)
            
        oPartDoc.Close(True)
    Next
'- - - - - - - - - - - - - Top Level routines - - - - - - - - - - - 

'Top Level Drawing
oAsmDrawing = ThisDoc.ChangeExtension(".idw")
oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmDrawing, True)
oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -4)
'write out the PDF for the Top Level Assembly Drawing file
On Error Resume Next ' if PDF exists and is open or read only, resume next
 'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & " V" & iProperties.Value("Project", "Revision Number") & ".pdf"
'Write out the PDF
Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
'Close the top level drawing
oAsmDrawingDoc.Close(True)

'Top Level IGES

Dim oData As DataMedium
oData = ThisApplication.TransientObjects.CreateDataMedium
oData.FileName = oFolder & "\" & oAsmName & " V" & iProperties.Value("Project", "Revision Number") & ".igs"
oIGESTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)


'Top Level STEP

Dim oSTEPData As DataMedium
oSTEPData = ThisApplication.TransientObjects.CreateDataMedium
oSTEPData.FileName = oFolder & "\" & oAsmName & " V" & iProperties.Value("Project", "Revision Number") & ".stp"
oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oSTEPContext, oSTEPOptions, oSTEPData)


'--------------------End of routine--------------------------------
'Create ZIP-file with created files
oZIPfolderCheck = oFolder & "\" & oAsmName & " V" & iProperties.Value("Project", "Revision Number") & ".zip"
oZIPContainer = oFolder & ".zip" 'Create file first in main folder and move after creation, because nesting of folders during creation produces errors
oZIPFinalDestination = oZIPfolderCheck

'Check if file already exists
If System.IO.File.Exists(oZIPfolderCheck)
    System.IO.File.Delete(oZIPfolderCheck)
End If

'Create File
System.IO.Compression.ZipFile.CreateFromDirectory(oFolder, oZIPContainer)

'Move File to correct folder
System.IO.File.Move(oZIPContainer,oZIPFinalDestination)

MessageBox.Show("Nieuwe bestanden in: " & vbLf & oFolder, "iLogic")
'open the folder where the new files are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)

End Sub
0 Likes