parts assembly to step with partnumber, qty, thick and material as filename

parts assembly to step with partnumber, qty, thick and material as filename

frederik
Explorer Explorer
235 Views
2 Replies
Message 1 of 3

parts assembly to step with partnumber, qty, thick and material as filename

frederik
Explorer
Explorer

hi there, 

 

can anyone help me, i have a ilogic rule that i like to run but it seems to get stuck on rule 27. woukld like to export the files from an assmebly to step fuiles with partnumber, qty, thicknes and material as the filename in a step folder

 

Sub Main
	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
	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

	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

	Dim oPath As String = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName)
	Dim oFolder As String = oPath & "\STEP Files"
	If Not System.IO.Directory.Exists(oFolder) Then
	    System.IO.Directory.CreateDirectory(oFolder)
	End If

	Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
	For Each oRefDoc As Document In oRefDocs
		'if is not a Part, skip to next referenced document
		If oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		Dim oCurFile As Document = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
		Dim oCurFileName = oCurFile.FullFileName
		Dim oPN As String = oCurFile.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
        Dim oQty As String = oCurFile.PropertySets.Item("Design Tracking Properties").Item("Quantity").Value
        Dim oThickness As String = oCurFile.PropertySets.Item("Physical Properties").Item("Thickness").Value
        Dim oMaterial As String = oCurFile.PropertySets.Item("Material").Item("Material").Value
        Dim ShortName = IO.Path.GetFileNameWithoutExtension(oCurFileName)
        Dim oNewFileName = oPN & " - " & oQty & " - " & oThickness & " - " & oMaterial & ".stp"
		'might want to ckeck if oPN = "" or not
		Try
			oCurFile.SaveAs(oFolder & "\" & oPN & ".stp", True)
		Catch
			MessageBox.Show("Error processing " & oCurFileName, "ilogic")
		End Try
		oCurFile.Close()
	Next
	MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
End Sub
0 Likes
236 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor

Hi @frederik.  I copied your existing code to my iLogic rule editor, edited a few things, and added some comments in there for you to review.  It is still not fully functioning the way you requested, but I am just showing you some of the things you will need to know to fix it.  The referenced documents in an open assembly are generally all already open, but invisibly, in the background, because they get 'loaded' (initialized) when you open the assembly.  It is a similar situation with a drawing, when you open it, it opens all of its referenced documents invisibly in the background, if they are not already open.  And you generally do not need for documents to be 'visibly' open to access their iProperties or Parameters by code either, so opening them invisibly is often the quicker choice to process.  Those documents being referenced by the assembly will remain open until the assembly is closed.  Only if you have opened them 'visibly' would you maybe want to try to close them, which would only make them invisible again, but not truly close them.  The quantity of something in an assembly is generally obtained from a specific row in the BOM (Bill of Materials) of the main assembly, not from some property of a referenced document.  That referenced document usually has no idea that it is being used in that parent assembly, only the assembly knows that it is included.  The thickness of a component would normally be retrieved from a Parameter object within the referenced document, not an iProperty, unless the Parameter has been set to 'Export', which creates a matching custom iProperty for it.

Sub Main
	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
	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

	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

	Dim oPath As String = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName)
	Dim oFolder As String = oPath & "\STEP Files"
	If Not System.IO.Directory.Exists(oFolder) Then
	    System.IO.Directory.CreateDirectory(oFolder)
	End If

	Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
	For Each oRefDoc As Document In oRefDocs
		'if is not a Part, skip to next referenced document
		If oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		'all of these documents should already be open (in the background, not necessarily visibly)
		'they get 'initialized' (similar to open) when you load the assembly
		'If Not oRefDoc.Open Then ThisApplication.Documents.Open(oRefDoc.FullDocumentName, False)
		Dim oPN As String = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
		'get Quantity from BOM row in main assy for this model (usually an Integer, but can be Double too)
        Dim oQty As String '= oAsmDoc.ComponentDefinition.BOM.BOMViews.Item(?).BOMRows.Item(?).ItemQuantity
		'you probably need to get Thickness from a Parameter within this oRefDoc, not iProperties
		'this line will throw an error if a parameter with that exact spelling, and capitalization is not found in oRefDoc
        Dim oThickness As String = oRefDoc.ComponentDefinition.Parameters.Item("Thickness").Value.ToString
        Dim oMaterial As String = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Material").Value
        Dim ShortName As String = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
        Dim oNewFileName = oPN & " - " & oQty & " - " & oThickness & " - " & oMaterial & ".stp"
		'might want to ckeck if oPN = "" or not
		Try
			oRefDoc.SaveAs(oFolder & "\" & oPN & ".stp", True)
		Catch
			MessageBox.Show("Error processing " & oCurFileName, "ilogic")
		End Try
		'oRefDoc.Close() 'can't really fully close them, because they are being referenced by assembly
		'if you opened them 'visibly', closing them will make them invisible, but still technically open
	Next
	MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

frederik
Explorer
Explorer

i also have this code where it takes al sheet metal parts to dxf and saves it with partnumber, qty, and thicknes.

but i dont know how to interate this to the step model

 

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension

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

'Get user input
RUsure = MessageBox.Show ( _
"This will create a DXF file for all of the asembly components that are sheet metal." _
& vbLf & "This rule expects that the part file is saved." _
& vbLf & " " _
& vbLf & "Are you sure you want to create DXF for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic  - Batch Output DXFs ", MessageBoxButtons.YesNo)

If RUsure <> vbYes Then Exit Sub

Dim oPath As String = ThisDoc.Path

'Get the DXF target folder path
Dim oFolder As String = oPath & "\" & oAsmName & " DXF Files"

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

'- - - - - - - - - - - - -Component  - - - - - - - - - - - -
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Dim oBOM As BOM
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
oBOM = oDoc.ComponentDefinition.BOM
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

oBOM.StructuredViewEnabled = True
Dim oBOMView As BOMView = oBOM.BOMViews.Item(oBOM.BOMViews.Count)
For Each oRow As BOMRow In oBOMView.BOMRows
Try
	Dim oCD As ComponentDefinition = oRow.ComponentDefinitions.Item(1)
	Dim iDoc As Document = oCD.Document
	'SheetMetal parts only
	If iDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
	Dim iName As String = iDoc.FullFileName

	'Check that model is saved
	If iName = vbNullString Then Continue For
	iDoc = ThisApplication.Documents.Open(iName)
	oCD = iDoc.ComponentDefinition
	
	Dim oItem As String = oRow.ItemNumber
	Try
		If Not oCD.HasFlatPattern Then
			oCD.Unfold()
		Else
			oCD.FlatPattern.Edit()
		End If

		Dim sOut As String
		
    	sOut = "FLAT PATTERN DXF?&AcadVersion=2004"
   		sOut = sOut & "&OuterProfileLayer=DECOUPE_EXTERNE"
    	sOut = sOut & "&InteriorProfilesLayer=DECOUPE_INTERNE"
    	sOut = sOut & "&FeatureProfilesLayer=GRAVURE"
    	sOut = sOut & "&FeatureProfilesUpLayerColor=255;0;0"
    	sOut = sOut & "&FeatureProfilesDownLayerColor=255;0;0"
    	sOut = sOut & "&UnconsumedSketchesLayer=GRAVURE_ESQUISSE"
    	sOut = sOut & "&UnconsumedSketchesLayerColor=255;0;0"
    	sOut = sOut & "&BendLayer=LIGNE_PLIAGE"
   		sOut = sOut & "&BendUpLayerColor=255;255;0"
    	sOut = sOut & "&BendUpLayerLineType=37644"
    	sOut = sOut & "&BendDownLayerColor=255;255;0"
    	sOut = sOut & "&BendDownLayerLineType=37644"
    	'sOut = sOut & "&ToolCenterLayer=CENTRE_OUTILS"
    	'sOut = sOut & "&ToolCenterUpLayerColor=255;0;255"
    	'sOut = sOut & "&ToolCenterDownLayerColor=255;0;255"
    	'sOut = sOut & "&ArcCentersLayer=CENTRE"
    	'sOut = sOut & "&ArcCentersLayerColor=255;0;255"
    	sOut = sOut & "&InvisibleLayers=IV_TANGENT;IV_ALTREP_FRONT;IV_ALTREP_BACK;IV_ROLL_TANGENT;IV_ROLL;IV_ARC_CENTERS"
    	sOut = sOut & "&SplineTolerance=0,001"
		
		Dim Qty As String = oRow.ItemQuantity()
		Dim ShortName As String = Left(iDoc.DisplayName, (InStrRev(iDoc.DisplayName, ".", -1, vbTextCompare) -1))
		Dim Thick As String = Parameter(iDoc.DisplayName, "Thickness")
		Dim ShortNameQTY As String = ShortName & " - D_ "  & Thick & " - A_" & Qty & " - " & ".dxf"

		oCD.DataIO.WriteDataToFile(sOut, oFolder & "\" & ShortNameQTY)
		oCD.FlatPattern.ExitEdit()
	Catch ex As Exception
		MsgBox(ex.Message)
	End Try
	iDoc.Close(True)

Catch
End Try
Next

 

0 Likes