Code for saving iparts as dxf flat sheet

Code for saving iparts as dxf flat sheet

bionorica2015
Enthusiast Enthusiast
419 Views
4 Replies
Message 1 of 5

Code for saving iparts as dxf flat sheet

bionorica2015
Enthusiast
Enthusiast

Hello

I have this code:

Sub CreateDxf()

Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition

Dim oPartMember As iPartMember

For Each oTab As iPartTableRow In oDef.iPartFactory.TableRows

MessageBox.Show(oTab.MemberName, "Name of the Member")

iPart.ChangeRow("", oTab.MemberName)

Dim oFeat As PartFeature
For Each oFeat In oDef.Features

If oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then

If oDef.HasFlatPattern = False Then
oDef.Unfold
Else
oDef.FlatPattern.Edit
End If

Dim odxf As String
Dim oPath As String

oPath = ThisDoc.Path & "\DXF"

If Not System.IO.Directory.Exists(oPath) Then
System.IO.Directory.CreateDirectory (oPath)
End If

Dim odxfname As String

odxf = "FLAT PATTERN DXF?OuterProfileLayer=0&OuterProfileLayerColor=0;0;0&InteriorProfilesLayer=0&InteriorProfilesLayerColor=0;0;0&BendDownLayerLineType=37633&BendDownLayerColor=0;0;255&BendUpLayerLineType=37633&BendUpLayerColor=0;0;255IV_BEND;IV_BEND_DOWN;IV_OUTER_PROFILE;IV_INTERIOR_PROFILES;IV_FEATURE_PROFILES;IV_FEATURE_PROFILES_DOWN;IV_ALTREP_FRONT;IV_ALTREP_BACK;IV_ROLL_TANGENT;IV_ROLL&InvisibleLayers=IV_TANGENT;IV_TOOL_CENTER;IV_TOOL_CENTER_DOWN;IV_ARC_CENTERS;IV_UNCONSUMED_SKETCHES" _
+ "&RebaseGeometry=True" _
+ "&SimplifySplines=True" _
+ "&SplineTolerance=0.01" _

odxfname = oPath & "\" & oTab.MemberName & ".dxf"
oDef.DataIO.WriteDataToFile(odxf, odxfname)
oDef.FlatPattern.ExitEdit

MessageBox.Show ("DXF has been saved in this location: " + odxfname)
Else
MessageBox.Show ("There must be a sheetmetal component to get the dxf file!")
End If

Next

Next
End Sub

 

But when I put it into vba to create macros these lines are getting red :

bionorica2015_1-1653242464475.pngbionorica2015_2-1653242482792.png

Please help what I must do to solve these problems?

PS: When I put it into ilogic rule these errors occur;

System.ArgumentException: Параметр задан неверно. (Исключение из HRESULT: 0x80070057 (E_INVALIDARG))
в System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
в Inventor.DataIO.WriteDataToFile(String Format, String FileName)
в ThisRule.Main()
в Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
в iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Yhis code is written for Inventor 2021. my Inventor is 2020

 

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

A.Acheson
Mentor
Mentor
Accepted solution

The original ilogic rule works . There was some feature looping which is likely not being used  so I removed that. Are you running this rule in the factory file? 

AAcheson_0-1653252271816.png

 

Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition

Dim oPartMember As iPartMember

For Each oTab As iPartTableRow In oDef.iPartFactory.TableRows

	MessageBox.Show(oTab.MemberName, "Name of the Member")

	iPart.ChangeRow("", oTab.MemberName)

	If oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then

		If oDef.HasFlatPattern = False Then
			oDef.Unfold
		Else
			oDef.FlatPattern.Edit
		End If

		Dim odxf As String
		Dim oPath As String

		oPath = ThisDoc.Path & "\DXF"

		If Not System.IO.Directory.Exists(oPath) Then
			System.IO.Directory.CreateDirectory (oPath)
		End If

		Dim odxfname As String

		odxf = "FLAT PATTERN DXF?OuterProfileLayer=0&OuterProfileLayerColor=0;0;0&InteriorProfilesLayer=0&InteriorProfilesLayerColor=0;0;0&BendDownLayerLineType=37633&BendDownLayerColor=0;0;255&BendUpLayerLineType=37633&BendUpLayerColor=0;0;255IV_BEND;IV_BEND_DOWN;IV_OUTER_PROFILE;IV_INTERIOR_PROFILES;IV_FEATURE_PROFILES;IV_FEATURE_PROFILES_DOWN;IV_ALTREP_FRONT;IV_ALTREP_BACK;IV_ROLL_TANGENT;IV_ROLL&InvisibleLayers=IV_TANGENT;IV_TOOL_CENTER;IV_TOOL_CENTER_DOWN;IV_ARC_CENTERS;IV_UNCONSUMED_SKETCHES" _
		+ "&RebaseGeometry=True" _
		+ "&SimplifySplines=True" _
		+ "&SplineTolerance=0.01" _

		odxfname = oPath & "\" & oTab.MemberName & ".dxf"
		oDef.DataIO.WriteDataToFile(odxf, odxfname)
		oDef.FlatPattern.ExitEdit

		MessageBox.Show ("DXF has been saved in this location: " + odxfname)
	Else
		MessageBox.Show ("There must be a sheetmetal component to get the dxf file!")
	End If
Next

 The VBA equivalent will take a lot of rework to remove System.IO reference and ilogic member change. 

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

A.Acheson
Mentor
Mentor

For the VBA equivalent. Here is some reference information for the System Object to check if the folder exist in windows.  To make it easier I added the  reference to "Microsoft Scripting Runtime" under Tools>References. This article  shows how to do add this reference. Without this reference you will need to know the exact method rather than having full access to the list of methods available under the object. 

Public Sub ExportiFactorytoDXF()
   Dim oDoc As PartDocument
   Set oDoc = ThisApplication.ActiveDocument
   
   Dim oDef As SheetMetalComponentDefinition
   Set oDef = oDoc.ComponentDefinition

    'Read the folder in Windows
    '*********************Early bindings*****************
 
    'Add reference to "Microsoft Scripting Runtime" under tools>References (https://wellsr.com/vba/2018/excel/introduction-to-the-vba-filesystemobject/)
    
    'Early bindings allows access to the object collection
    Dim fs As FileSystemObject
    Set fs = New FileSystemObject
     '******************Late bindings*********
    'Dim fs As Object
    'Set fs = CreateObject("Scripting.FileSystemObject")
     
     'Create the folder path
     Dim oPath As String
     oPath = fs.GetParentFolderName(oDoc.FullFileName) & "\DXF"
    
    'check if the folder exist and if not create
    If fs.FolderExists(oPath) = False Then
        fs.CreateFolder (oPath)
    End If
    
    Dim oRow As iPartTableRow
    For Each oRow In oDef.iPartFactory.TableRows
    
    'Change the member
    oDef.iPartFactory.DefaultRow = oRow
   ' MsgBox(oRow.MemberName, "Name of the Member")

    If oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then

        If oDef.HasFlatPattern = False Then
            oDef.Unfold
        Else
            oDef.FlatPattern.Edit
        End If

        Dim odxf, odxfname As String
       
        odxf = "FLAT PATTERN DXF?OuterProfileLayer=0&OuterProfileLayerColor=0;0;0&InteriorProfilesLayer=0&InteriorProfilesLayerColor=0;0;0&BendDownLayerLineType=37633&BendDownLayerColor=0;0;255&BendUpLayerLineType=37633&BendUpLayerColor=0;0;255IV_BEND;IV_BEND_DOWN;IV_OUTER_PROFILE;IV_INTERIOR_PROFILES;IV_FEATURE_PROFILES;IV_FEATURE_PROFILES_DOWN;IV_ALTREP_FRONT;IV_ALTREP_BACK;IV_ROLL_TANGENT;IV_ROLL&InvisibleLayers=IV_TANGENT;IV_TOOL_CENTER;IV_TOOL_CENTER_DOWN;IV_ARC_CENTERS;IV_UNCONSUMED_SKETCHES" _
        + "&RebaseGeometry=True" _
        + "&SimplifySplines=True" _
        + "&SplineTolerance=0.01" _

        odxfname = oPath & "\" & oRow.MemberName & ".dxf"
        Call oDef.DataIO.WriteDataToFile(odxf, odxfname)
        oDef.FlatPattern.ExitEdit

        MsgBox ("DXF has been saved in this location: " + odxfname)
    Else
        MsgBox ("There must be a sheetmetal component to get the dxf file!")
    End If
Next
End Sub

 

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

bionorica2015
Enthusiast
Enthusiast

Invalid argument in this line:

Call oDef.DataIO.WriteDataToFile(odxf, odxfname)

 

Tools-refernce-Microsoft Scriptig Runtime I put checked

0 Likes
Message 5 of 5

A.Acheson
Mentor
Mentor

Maybe you need to set the DataIO object as per the API sample here.

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