Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Upgrading Export DXF flat pattern from Assembly with iProperty naming

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
vkulikajevas
2091 Views, 12 Replies

Upgrading Export DXF flat pattern from Assembly with iProperty naming

I need to Use this Rule (Or similar) to generate flat patterns from assembly sheet files. Problem is I can't figure it out how to make this rule to name all created DXF files to using every part info:
Description (From the sheet part) & " "  & Material (From the sheet part) & " " Stock Number (From the sheet part) " " QTY (From the assembly - i.d. QTY used in assembly of each part).

 

'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 ( _
"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 = vbNo Then
Return
Else
End If
oPath = ThisDoc.Path
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
'get DXF target folder path
oFolder = 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 - - - - - - - - - - - -'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 been saved
For Each oRefDoc In oRefDocs
iptPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "ipt"
'check that model is saved
If(System.IO.File.Exists(iptPathName)) Then
Dim oDrawDoc As PartDocument
oDrawDoc = ThisApplication.Documents.Open(iptPathName, True)
oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName))
Try
'Set the DXF target file name

Try
CustomName =iProperties.Value(oFileName, "Custom", "PF_PRT_ZNR")
Catch
CustomName ="XXX" 'Wert, wenn iPropertie PF_PRT_ZNR nicht existiert
End Try

oDataMedium.FileName = oFolder & "\" & CustomName  & " " & oFileName & ".dxf"

Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oDrawDoc.ComponentDefinition
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold
Else
oCompDef.FlatPattern.Edit
End If
Dim sOut As String
sOut = "FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=IV_OUTER_PR?OFILE"
oCompDef.DataIO.WriteDataToFile( sOut, oDataMedium.FileName)
'just for check its works coretcly'i=MessageBox.Show(oDataMedium.FileName, "Title",MessageBoxButtons.OKCancel)'MessageBox.Show(i,"title",MessageBoxButtons.OK)'If i=2 Then'Exit Sub'End If
oCompDef.FlatPattern.ExitEdit
Catch
End Try
oDrawDoc.Close
Else
End If
Next

 

12 REPLIES 12
Message 2 of 13
JelteDeJong
in reply to: vkulikajevas

The short answer:

long aswer i did have a look at your code. i implemented the properties en material in the filename of the dxf.

Also i found a a couple of things that could be done better. I added comments to the code and  changed it in a way i think is better.

 

'check that the active document is an assembly file
        '--> first check if the is an assembly before you declare it to an AssemblyDocument
        If ThisApplication.ActiveDocument.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
            MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
            Exit Sub
        End If

        'define the active document as an assembly file
        Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
        '--> oAsmDoc.DisplayName can show the extension depending on windows settings.
        '--> i guess that you try to remove the extensin here.
        '--> if you do it in this way your colega, with different windows settings, will have an unexpexted resilt. 
        'Dim oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) - 4)
        Dim oAsmName = oAsmDoc.DisplayName.Replace(".iam", "")


        'get user input
        Dim 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 = vbNo Then
            Return
            ' Else <-- this is not needed.
        End If

        Dim oPath = ThisDoc.Path

        '--> it seems that you use oDataMedium only for the file path.
        '--> you might want to have a look at the k.i.s.s. design principl 
        '--> https://en.wikipedia.org/wiki/KISS_principle
        '--> i think you wont need it.
        'Dim oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

        '--> oContext is declared her but is never used...
        'Dim oContext = ThisApplication.TransientObjects.CreateTranslationContext
        'oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

        '--> oOptions is declared her but is never used...
        'Dim oOptions = ThisApplication.TransientObjects.CreateNameValueMap

        'get DXF target folder path
        Dim oFolder = 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 - - - - - - - - - - - -
        'look at the files referenced by the assembly
        Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments

        'work the the drawing files for the referenced models'this expects that the model has been saved
        For Each oRefDoc As Document In oRefDocs

            '--> this seem to replase the extension of the filename. Then you try to check with this 
            '--> if it is saved (and probaly if its an part and not an assembly)
            '--> you can better use the Document.Dirty property. this will be false if the file is not saved.
            '--> also if the file exists but was not saved.
            '--> for checking if its a part we can use the Document.DocumentType.
            '--> last i did not see if you checked if it is realy a sheet metal part. 
            '--> we can do that with the Document.SubType

            'Dim iptPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "ipt"
            'check that model is saved
            'If (System.IO.File.Exists(iptPathName)) Then

            '--> check if its a part:
            If (oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then
                Continue For '--> this will make the program go to the next oRefDoc
            End If
'--> check if its a Sheetmetal part: If (oRefDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}") Then Continue For '--> this will make the program go to the next oRefDoc End If
'--> check if its saved: If (oRefDoc.Dirty = True) Then '--> TODO ask the user if he/she wants to save? '--> oRefDoc.Save() Continue For '--> this will make the program go to the next oRefDoc End If
Dim oDrawDoc As PartDocument = ThisApplication.Documents.Open(oRefDoc.FullFileName, True) Dim oFileName = oRefDoc.DisplayName.Replace(".ipt", "") Dim oCompDef As SheetMetalComponentDefinition = oDrawDoc.ComponentDefinition Try 'Set the DXF target file name '--> you need to declare the CustomName before the try/cath or you will only be able to use its value in try/cath statement '--> i also declared the other values that you asked for. (with there default values so i dont have to do it in the cath block) Dim customPropertiesSet = oRefDoc.PropertySets.Item("Inventor User Defined Properties") Dim customName As String = "XXX" Try customName = customPropertiesSet.Item("PF_PRT_ZNR").Value Catch '--> TODO create a msgbox to want the user that the name can be starnge 'customName = "XXX" 'Wert, wenn iPropertie PF_PRT_ZNR nicht existiert End Try Dim designTrackingPropertiesSet = oRefDoc.PropertySets.Item("Design Tracking Properties") Dim description As String = designTrackingPropertiesSet.Item("Description").Value Dim StockNumber As String = designTrackingPropertiesSet.Item("Stock Number").Value Dim Material As String = oCompDef.ActiveSheetMetalStyle.Material.Name 'oDataMedium.FileName = oFolder & "\" & CustomName & " " & oFileName & ".dxf" Dim newFileName As String = oFolder & "\" & customName & " " & description & " " & StockNumber & " " & Material & ".dxf" If oCompDef.HasFlatPattern = False Then oCompDef.Unfold() Else oCompDef.FlatPattern.Edit() End If Dim sOut As String sOut = "FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=IV_OUTER_PR?OFILE" oCompDef.DataIO.WriteDataToFile(sOut, newFileName) 'oCompDef.DataIO.WriteDataToFile(sOut, oDataMedium.FileName) oCompDef.FlatPattern.ExitEdit() Catch End Try oDrawDoc.Close() 'Else 'End If Next

:

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 13
vkulikajevas
in reply to: JelteDeJong

The code works really well! Thank you very much!

Message 4 of 13
vkulikajevas
in reply to: JelteDeJong

JelteDeJong, there is something wrong with the code when writing material to the parts.

It always writes AISI 304 instead of material that I applied to parts. Maybe you could have a fix for that? 🙂

Prob_02.JPGProblem01.JPGProblem_Material.JPG

Message 5 of 13
JelteDeJong
in reply to: vkulikajevas

My code does work but maby not as you expected probaly. It's a bit confusing but for sheetmetal parts the matrial that you can select in the top of your screen is an override from the material in the sheetmatal rules. I think it's best to use the sheetmatal rules to set the material because then it's possible to also set material thickness, k-factor's, and other stuff that is importend for sheetmetal parts in 1 click.

That being said if you want to use the override then change the code that get the material in this:

'Dim Material As String = oCompDef.ActiveSheetMetalStyle.Material.Name
Dim Material As String = oRefDoc.ActiveMaterial.DisplayName

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 6 of 13
aurel_e
in reply to: JelteDeJong

@JelteDeJong 

Is there any way to run this ilogic rule "silently"? I mean without opening every sheet metal file. Actually it opens them save the flat pattern and close them. Can we change the code to make it run in background? 

Message 7 of 13
aurel_e
in reply to: aurel_e

I think I figured it out 

oDrawDoc = ThisApplication.Documents.Open(iptPathName, True) -> False

 

Message 8 of 13
aurel_e
in reply to: aurel_e

Another question: is it possible to set the dxf layers? For instance: I would like to have the UNCONSUMED SKETCHES layer name Yellow, color Yellow.

Message 9 of 13
JelteDeJong
in reply to: aurel_e

hi it's possible you will need to change the option string (sOut). in your script replace the line:

sOut = "FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=IV_OUTER_PR?OFILE"

with (see also comments in code):

Dim optionList As List(Of String) = New List(Of String)()
optionList.add("AcadVersion=2004") optionList.add("OuterProfileLayer=IV_OUTER_PR") optionList.add("UnconsumedSketchesLayerColor=255;0;0") 'here i make the UnconsumedSketchesLayer lines red! optionList.add("") 'you can add other options here optionList.add("") 'and more options ' if you want you can add more lines for more options
Dim sOut As String sOut = "FLAT PATTERN DXF?" For Each opt In optionList sOut = sOut & "&" & opt Next sOut = sOut & "?OFILE"

in the new code you can add option like they are discribed in the help files (search for: "Translate - Sheet Metal to DXF API Sample").

help translate api.png

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 10 of 13
aurel_e
in reply to: JelteDeJong

@JelteDeJong  Thank you very much.

It works. This is the final part of my rule: 

oCompDef = oDrawDoc.ComponentDefinition

Dim optionList As List(Of String) = New List(Of String)()

optionList.Add("AcadVersion=2004")
optionList.Add("OuterProfileLayer=0")
optionList.Add("InteriorProfilesLayer=0")
optionList.Add("UnconsumedSketchesLayer=YELLOW")
optionList.add("UnconsumedSketchesLayerColor=255;255;0") 'here i make the UnconsumedSketchesLayer lines red!

Dim sOut As String
sOut = "FLAT PATTERN DXF?AcadVersion=2000" _
+ "&InvisibleLayers=IV_TANGENT;IV_FEATURE_PROFILES_D​OWN;IV_ARC_CENTERS;IV_BEND;IV_BEND_DOWN;IV_TOOL_CENTER;IV_TOOL_CENTER_DOWN;IV_FEATURE_PROFILES;	IV_FEATURE_PROFILES_DOWN" 

 
For Each opt In optionList
	sOut = sOut & "&" & opt
Next
sOut = sOut & "?OFILE"
oCompDef.DataIO.WriteDataToFile( sOut, oDataMedium.FileName)

oCompDef.FlatPattern.ExitEdit
Catch
End Try
oDrawDoc.Close
Else
End If
Next
Message 11 of 13
aurel_e
in reply to: JelteDeJong

@JelteDeJong 

Something is not working properly:

I have an assembly with some Ipt that are derived parts. The code is exporting the derived part sources  as well.

Can we make the code to avoid it?

I would like the code to export just the components in this LOD, ignoring the suppressed parts as well.

Thank you.

Message 12 of 13
Anonymous
in reply to: JelteDeJong

I have assembly contain sub assembly, how can activate every part (as I assembly) inside this assembly or sub assembly and export dxf for each sheet metal parts

Message 13 of 13

this code seems to be skipping a few parts and i have no idea why ?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report