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
DimoAssDocAsAssemblyDocumentoAssDoc = ThisApplication.ActiveDocument
DimoAssDefAsAssemblyComponentDefinitionoAssDef = oAssDoc.ComponentDefinition
DimoCompAsComponentOccurrence
For Each oComp In oAssDef.Occurrences
oComp.Edit()
oComp.ExitEdit(ExitTypeEnum.kExitToTop)
Next
DimopenDocAsDocumentopenDoc = ThisDoc.Document
DimdocFileAsDocument
'if this is an assembly document
If openDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
For EachdocFile 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
DimsFnameAsString
sOut = "FLAT PATTERN DWG?AcadVersion=2004
&RebaseGeometry=True
&OuterProfileLayer=DIGI_CUTTING_TOOL_2
&OuterProfileLayerColor=102;102;102
&InteriorProfilesLayer=DIGI_CUTTING_TOOL_1
&InteriorProfilesLayerColor=255;255;255
&BendDownLayer=DIGI_MARKER_TOOL_2
&BendDownLayerColor=255;127;223
&BendUpLayer=DIGI_MARKER_TOOL_1
&BendUpLayerColor=0;255;0
&InvisibleLayers=IV_ARC_CENTERS;IV_TANGENT;IV_ROLL;IV_ROLL_TANGENT;IV_ALTREP_BACK;IV_ALTREP_FRONT;
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")
EndIf