Drive Rule in Sub Sheet Metal part to Export DWG/DXF by DataIO

By Xiaodong Liang

These days, I got one case, where one assembly includes some sheet metal parts. In each part, there are some rules, one of which exports the document to DWG/DXF, another exports to other file format. In the assembly, one rule iterates each parts and executes the rules.

It works well with other format. i.e. after running rule of assembly, the corresponding files are generated. But DWG/DXF are not generated. An unspecific error occurred. If activating the parts one by one and run the assembly rule again, the DWG/DXF files can be generated now.

I did a couple of test and found the problem is because of the way of exporting. For other format, the rule uses TranslatorAddin, while for DWG/DXF, the rule uses SheetMetalComponentDefinition.DataIO.WriteDataToFile
while it requires to in the editing mode of sheet metal: SheetMetalComponentDefinition.FlatPattern.Edit.
The code failed at FlatPattern.Edit. It looks if the sheet metal part has not been explicitly activated or opened, the flat pattern mode cannot be edited.

Finally, the workaround is to activate the components one by one (like activate manually) before running the rule will edit the flat pattern.

This is the test assembly and part: Download TestData

The relevant rules are as below:

Assembly Rule:
'for the rule that will edit flat pattern,
' activate the parts in advance
Dim oAssDoc As AssemblyDocument
oAssDoc = ThisApplication.ActiveDocument
Dim oAssDef As AssemblyComponentDefinition
oAssDef = oAssDoc.ComponentDefinition
Dim oComp As ComponentOccurrence
For Each oComp In oAssDef.Occurrences
    oComp.Edit()
    oComp.ExitEdit(ExitTypeEnum.kExitToTop)
Next
Dim openDoc As Document
openDoc = ThisDoc.Document
Dim docFile As Document
'if this is an assembly document
If openDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
    For Each docFile In openDoc.AllReferencedDocuments
        Dim FNamePos As Long
        FNamePos = InStrRev(docFile.FullFileName, "", -1)
        Dim docFName As String
        docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)
        'drive the sub rule
        iLogicVb.RunRule(docFName, "exportDWG_DXF_ByDataIO")
    Next
Else
    MessageBox.Show("This is not an assembly document", "Error")
End If
Sheet Metal Part Rule:
Dim curDoc = ThisDoc.Document
'if this is a sheet metal part
If curDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
    Dim oCompDef As SheetMetalComponentDefinition
    oCompDef = curDoc.ComponentDefinition
    'edit the flat pattern
    oCompDef.FlatPattern.Edit
    Dim sOut As String
    Dim sPATH As String
    sPATH = ThisDoc.Path
    Dim sFname As String
    sOut = "FLAT PATTERN DWG?AcadVersion=2004" & vbCrLf & _
           "&RebaseGeometry=True" & vbCrLf & _
           "&OuterProfileLayer=DIGI_CUTTING_TOOL_2" & vbCrLf & _
           "&OuterProfileLayerColor=102;102;102" & vbCrLf & _
           "&InteriorProfilesLayer=DIGI_CUTTING_TOOL_1" & vbCrLf & _
           "&InteriorProfilesLayerColor=255;255;255" & vbCrLf & _
           "&BendDownLayer=DIGI_MARKER_TOOL_2" & vbCrLf & _
           "&BendDownLayer=DIGI_MARKER_TOOL_2" & vbCrLf & _
           "&BendDownLayerColor=255;127;223" & vbCrLf & _
           "&BendUpLayer=DIGI_MARKER_TOOL_1" & vbCrLf & _
           "&BendUpLayerColor=0;255;0" & vbCrLf & _
           "&InvisibleLayers=IV_ARC_CENTERS;IV_TANGENT;IV_ROLL;IV_ROLL_TANGENT;IV_ALTREP_BACK;IV_ALTREP_FRONT;" & vbCrLf & _
           "IV_FEATURE_PROFILES_DOWN;IV_FEATURE_PROFILES;IV_TOOL_CENTER_DOWN"
    sFname = sPATH & "" & ThisDoc.FileName(False) & ".dwg"
    'sOut = "FLAT PATTERN DXF?AcadVersion=R12&OuterProfileLayer=Outer"
    'sFname = sPATH & "" & ThisDoc.FileName(False) & ".dxf"
    oCompDef.DataIO.WriteDataToFile(sOut, sFname)
    oCompDef.FlatPattern.ExitEdit
Else
    MessageBox.Show("This is not a sheet metal part!", "Error")
End If

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading