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

(Not an Autodesk Employee)