Hello all,
I have found some code a while back that will create a DXF when I save a sheet metal file. I recently noticed though that if I am in an assembly and a sheet metal component needs saved, I get the following error. I am only firing the rule to run on save in the sheet metal components. This is an external rule.
Line 12 is:
oDoc = ThisApplication.ActiveDocument
I assume this has to deal with how the code is referencing documents. All i am wanting is the rule to run on the sheet metal parts only and if there are mods then make sure they are also run behind the scenes and not bark at me at an assembly level lol...
Here is the code:
SETFilePath = ThisDoc.Path Dim partDoc As PartDocument If ThisApplication.ActiveDocument.DocumentType <> kPartDocumentObject Then MessageBox.Show ("Please open a part document", "iLogic") End If 'Check for flat pattern >> create one if needed Dim oDoc As PartDocument oDoc = ThisApplication.ActiveDocument Dim oCompDef As SheetMetalComponentDefinition oCompDef = oDoc.ComponentDefinition If oCompDef.HasFlatPattern = False Then oCompDef.Unfold Else oCompDef.FlatPattern.Edit End If 'DXF Settings Dim sOut As String Dim sPATH As String 'sOut = "FLAT PATTERN DXF?AcadVersion=2000&OuterProfileLayer=IV_INTERIOR_PROFILES" sOut = "FLAT PATTERN DXF?AcadVersion=2000&OuterProfileLayer=IV_OUTER_PROFILE&InvisibleLayers=IV_TANGENT;IV_ROLL_TANGENT" Dim sFname As String sFname = SETFilePath & "\" & ThisDoc.FileName(False) & ".dxf" 'Export the DXF and fold the model back up oCompDef.DataIO.WriteDataToFile( sOut, sFname) Dim oSMDef As SheetMetalComponentDefinition oSMDef = oDoc.ComponentDefinition oSMDef.FlatPattern.ExitEdit
Thanks in advanced!
You could try using "ThisDoc.Document" instead of "ThisApplication.ActiveDocument". try it this way:
Dim SETFilePath = ThisDoc.Path
Dim doc As Document = ThisDoc.Document
Dim partDoc As PartDocument
If doc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
MessageBox.Show("Please open a part document", "iLogic")
End If
If (doc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}") Then
MessageBox.Show("Please open a sheetmetal document", "iLogic")
End If
'Check for flat pattern >> create one if needed
Dim partDocument As PartDocument = doc
Dim oCompDef As SheetMetalComponentDefinition = partDocument.ComponentDefinition
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold()
Else
oCompDef.FlatPattern.Edit()
End If
'DXF Settings
Dim sOut As String
Dim sPATH As String
'sOut = "FLAT PATTERN DXF?AcadVersion=2000&OuterProfileLayer=IV_INTERIOR_PROFILES"
sOut = "FLAT PATTERN DXF?AcadVersion=2000&OuterProfileLayer=IV_OUTER_PROFILE&InvisibleLayers=IV_TANGENT;IV_ROLL_TANGENT"
Dim sFname As String = SETFilePath & "\" & ThisDoc.FileName(False) & ".dxf"
'Export the DXF and fold the model back up
oCompDef.DataIO.WriteDataToFile(sOut, sFname)
Dim oSMDef As SheetMetalComponentDefinition
oSMDef = partDocument.ComponentDefinition
oSMDef.FlatPattern.ExitEdit()
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.
Blog: hjalte.nl - github.com
It sounds like you have this code linked to a save file trigger - is that correct? The intention being that every time you save, it generates a DXF file?
If that's correct, I suggest you move this code to form part of your approval workflow. The problem with creating a DXF every time you save is that it wastes time. You should be saving documents every few minutes, so that's a lot of time spent waiting (even if it is only a few seconds each time). Total time spent per hour = Number of documents saved X Time per save X Saves per hour.
If that's NOT correct, then I assume you have it connected to a button, and you're just pressing it while your active document is an assembly.
As far as the error you're receiving is concerned, the error occurs because the code expects that the active document is a sheet metal part (Line 3) but it isn't. Line 15 requires that the document have a subtype of sheetmetal.
Assuming you're running this from a button, you'll need to update the code to handle different active document types.
Here's my take on the code:
Sub Main()
Dim doc As Document = ThisDoc.Document
Select Case doc.DocumentType
Case kpartdocumentobject
Dim partDoc As PartDocument
If doc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
MessageBox.Show("Please open a part document", "iLogic")
End If
If (doc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}") Then
MessageBox.Show("Please open a sheetmetal document", "iLogic")
End If
If doc.DocumentType = kpartdocumentobject And doc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
SaveFlatPattern(doc)
End If
Case kassemblydocumentobject
For Each oSubDoc As Document In doc.AllReferencedDocuments
If oSubDoc.DocumentType = kpartdocumentobject And oSubDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
saveFlatPattern(oSubDoc)
End If
Next oSubDoc
Case Else
MessageBox.Show("Please select a part or assembly document", "Save DXF")
End Select
End Sub
Sub SaveFlatPattern(doc As PartDocument)
Dim SETFilePath = ThisDoc.Path
'Check for flat pattern >> create one if needed
Dim oCompDef As SheetMetalComponentDefinition
oCompDef = doc.ComponentDefinition
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold()
Else
oCompDef.FlatPattern.Edit()
End If
'DXF Settings
Dim sOut As String
Dim sPATH As String
'sOut = "FLAT PATTERN DXF?AcadVersion=2000&OuterProfileLayer=IV_INTERIOR_PROFILES"
sOut = "FLAT PATTERN DXF?AcadVersion=2000&OuterProfileLayer=IV_OUTER_PROFILE&InvisibleLayers=IV_TANGENT;IV_ROLL_TANGENT"
Dim sFname As String = doc.FullFileName.Replace(".ipt",".dxf")'SETFilePath & "\" & ThisDoc.FileName(False) & ".dxf"
'Export the DXF and fold the model back up
oCompDef.DataIO.WriteDataToFile(sOut, sFname)
Dim oSMDef As SheetMetalComponentDefinition
oSMDef = doc.ComponentDefinition
oSMDef.FlatPattern.ExitEdit()
End Sub
I've split out the DXF generation as a subroutine, and added some file type handling to the Main() subroutine.
EDIT: This code requries that the DXF Filename is available for write access - e.g. it's not open in AutoCAD or elsewhere. If required, you could add some error handling around that aspect.
doc.FullFileName.Replace(".ipt",".dxf")
Hope that helps.
Thanks to @JelteDeJong and @gerrardhickson for the responses. I am currently out of town and have not had a chance to look at the options yet.
You are correct. I have the "ON SAVE" to run this external rule to generate a dxf upon save. I did this so the users wouldn't have to remember to save the dxf, so eliminating an out of date file. Kind of just forcing the update. All seemed to work fine when updating the part file (no errors). But when having an assembly open and lets say you mod a part, but didnt save it then, and went back to the assembly did the update, then saved. That is when an error would throw. As @JelteDeJong had mentioned I wondered if it had to do with how the docs were being define, but I'm not really a programmer, just know enough. So, when I get a chance, I will look into the options.
thanks again.
Can't find what you're looking for? Ask the community or share your knowledge.