Exporting .dwg

Exporting .dwg

JonnyPot
Advocate Advocate
931 Views
7 Replies
Message 1 of 8

Exporting .dwg

JonnyPot
Advocate
Advocate

Hello everyone,

 

i have made a rull that exports the connected drawing of each part in a .dwg format with the part number as its name. my problem is when theres parts with difrent model states, i have tried adding a If statement but it always ends in error, if anyone could help me it would be much appreciated.

 

thank  you in advance.

 

Dim doc As AssemblyDocument = ThisDoc.Document
Dim refDoc As Document = Nothing
Dim Path As String = ThisDoc.Path
For Each refDoc In doc.AllReferencedDocuments
	
	Dim PartNumber As String = iProperties.Value(refDoc.DisplayName,"Project", "Part Number")
    Dim fileName As String = refDoc.FullFileName
    Dim ext As String = IO.Path.GetExtension(fileName)
	Dim FP As String = IO.Path.GetFullPath(fileName)
	Dim dwgFileName As String = fileName.Replace(FP, Path)
    Dim idwFileName As String = fileName.Replace(ext, ".idw")

    If (IO.File.Exists(idwFileName) = False) Then
       MsgBox("Could not find inventor idw: " & idwFileName)
       Continue For
    End If

    Dim dwgDoc As DrawingDocument = ThisApplication.Documents.Open(idwFileName)
    dwgDoc.SaveAs(dwgFileName & "\Drawings\" & PartNumber & ".dwg" , True)
    dwgDoc.Close(True)
Next

 

 

0 Likes
Accepted solutions (1)
932 Views
7 Replies
Replies (7)
Message 2 of 8

theo.bot
Collaborator
Collaborator

When you take the part number directly from the referenced file, you will avoid the error. So don't use the ilogic snippet, but use the property sets.

	Dim PartNumber As String '= iProperties.Value(refDoc.FullFileName, "Project", "Part Number")
	PartNumber = refDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value

 

Message 3 of 8

WCrihfield
Mentor
Mentor

If you run the rule as posted above, what error message(s) are you seeing?  Can you post the screen captured images of both tabs of the error message here, so we can see them?  Are you absolutely sure that every referenced document has the Part Number iProperty filled in with something?  If not, it will be trying to save the DWG with no file name between the directory separator character and the ".dwg" extension.  You can include a line of code after getting the part number that checks to make sure a value (other than an empty string) was retrieved, just to be sure.

 

Have you tried any debugging techniques yet to narrow down which line of code it is stopping at.  One of the simplest & oldest tricks is to put a line of code to show a message (MsgBox(1), or MessageBox.Show(1)) with a simple but unique message in it after each line of code.  Then when you run the rule, the last message you see will indicate that the next line of code after where that message is, is where the code is encountering an error.  That line may or may not be the cause of the error, but it's a starting point.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 8

JonnyPot
Advocate
Advocate

i am sorry if my post is a bit confusing 

 

the rule above works well when there's not any model states, but when there are i get this error

 

JonnyPot_1-1636494823913.png

 

i tried to add a statement that if the refDoc is a Model state to open its drawing, change the views to the correct model state, and then export it in .dwg. But I kind of found wall and can't figure out who to do it

 

thank you for the replay

 

 

 

 

 

0 Likes
Message 5 of 8

theo.bot
Collaborator
Collaborator
When you replace the lines that i replied earlier it works again. It goes wrong when you use the standard snippet to get the iproperty. I tested it.
Message 6 of 8

theo.bot
Collaborator
Collaborator

You can also use this if statement if you would like to use the ilogic snippets for properties:

 

Dim PartNumber As String 
If refDoc.ModelStateName <> "Master" Then
PartNumber = iProperties.Value(Left(refDoc.DisplayName,Len(refDoc.DisplayName)-Len(refDoc.ModelStateName)-3), "Project", "Part Number")
Else
PartNumber = iProperties.Value(refDoc.DisplayName, "Project", "Part Number")
End If

 

Message 7 of 8

WCrihfield
Mentor
Mentor
Accepted solution

I think I may have a complete new code for you to try.  Your last post where the document's name included the model state's name gave me the clue I was looking for.  When the document is set to a ModelState other than 'Master', that means it is a ModelState Member, and not a regular document with a standalone file path & file name.  So, to get the actual document that has a file path & file name, we need to get what is being called its 'FactoryDocument', which is basically the version of the document when it is set to its 'Master' model state.  To get that FactoryDocument, we need to first dig down into either the PartComponentDefinition or AssemblyComponentDefinition of that referenced document, then that FactoryDocument is a Property of those two specific types of ComponentDefinition objects when it is set to a non Master model state.  I believe we can then simply re-set the value of our oRefDoc variable to that FactoryDocument, so that we are using that through the rest of the loop's code.

 

Then I am also accessing the iProperty through normal API means (instead of using the iLogic snippet), because when accessing iProperties within other documents (instead of other components by component name) that iLogic snippet is simply not as reliable or efficient.  I also completely re-coded the file path & file naming portion, because what you had before seemed wrong to me.  If I understand your intent correctly about where the IDW file should be, and where you want to save the new DWG file to, then I believe the new code for specifying those paths & names should do the trick. Just to clarify though, I believe that the IDW file is in the same location and has the same file name as the referenced document, just with the ".idw" file extension, right?  Then you want the DWG file to be saved under the same folder where the main assembly's file is, but in a sub folder named "Drawings", then the file name is to be the Part Number, followed by the ".dwg" file extension, right?  If both assumptions are correct, then I believe that part of the code should be OK going forward.

 

Try the following complete code and see if this will now work for you.

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim Path As String = ThisDoc.Path
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	If oRefDoc.ModelStateName <> "Master" Then
		'it is not currently set to its 'Master' ModelState, so we may not have a true file path (ModelStateMember)
		'we will likely have to get its FactoryDocument from its ComponentDefinition
		If oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
			oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			oRefDoc = oRefDoc.ComponentDefinition.FactoryDocument
		End If
	End If
			
	Dim PartNumber As String = oRefDoc.PropertySets.Item(3).Item("Part Number").Value
	If String.IsNullOrEmpty(PartNumber) Then
		MsgBox("Part Number was empty in following file:" & vbCrLf & _
		oRefDoc.FullFileName, vbInformation, "iLogic")
		Continue For 'skip to next oRefDoc
	End If
	'get full file name, but with different file extension
	oIDWFileName = System.IO.Path.ChangeExtension(oRefDoc.FullFileName, ".idw")
	'get file name (without path & without file extension)
	oName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
	
    If Not System.IO.File.Exists(oIDWFileName) Then
       MsgBox("Could not find inventor idw: " & oIDWFileName)
       Continue For
    End If
	
	oDWGsFolder = oPath & "\Drawings\"
	'make sure that 'Drawings' folder/directory exists
	If Not System.IO.Directory.Exists(oDWGsFolder) Then
		System.IO.Directory.CreateDirectory(oDWGsFolder)
	End If
	
	'put DWG file name together (using path of main assembly, instead of IDW's same path)
	oDWGFileName = oDWGsFolder & PartNumber & ".dwg"
	
    Dim dwgDoc As DrawingDocument = ThisApplication.Documents.Open(oIDWFileName)
    dwgDoc.SaveAs(oDWGFileName, True)
    dwgDoc.Close(True)
Next

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 8

JonnyPot
Advocate
Advocate

thanks a lot for the time you tuck to make this rule. I also appreciate the part where it creates a new folder if there isn't one, that was something that i was planning to implement. Have a wonderful day.

0 Likes