AnyCAD structure with original file names

<?xml encoding=”UTF-8″>By Adam Nagy

Inside Inventor 2016 you can reference other CAD file formats directly from an assembly. In this case you can also see the structure of the referenced assembly in the model tree. If you’d also need to get back the structure using the original file names – i.e. going from Occurrence to the original file – that is currently not exposed in the API: no direct access from “SubOccurrence1” to “AnyCAD Part1” in the blow picture.

Image001-6

One workaround could be finding the original files based on the Occurrence.Name, since both that and the Inventor document names created from the foreign file are based on the name of the original file. You can find the list of the original file names through Document.File.AllReferencedFiles:

Function GetFileName(filePath As String) As String
Dim i1 As Integer
i1 = InStrRev(filePath, "")
Dim i2 As Integer
i2 = InStrRev(filePath, ".")
' Check for occurrence name as well
If i2 < 1 Then i2 = InStrRev(filePath, ":")
GetFileName = Mid(filePath, i1 + 1, i2 - i1 - 1)
End Function
Function GetFullPath(fileName As String) As String
Dim asm As AssemblyDocument
Set asm = ThisApplication.ActiveDocument
Dim f As File
For Each f In asm.File.AllReferencedFiles
If GetFileName(f.FullFileName) = fileName Then
GetFullPath = f.FullFileName
Exit Function
End If
Next
End Function
Sub PrintOccurrences( _
occs As ComponentOccurrences, _
i As Integer)
Dim occ As ComponentOccurrence
For Each occ In occs
Debug.Print Space(i); occ.name
Dim doc As Document
Set doc = occ.Definition.Document
Dim fullPath As String
If doc.IsEmbeddedDocument Then
Dim fileName As String
fileName = GetFileName(occ.name)
fullPath = GetFullPath(fileName)
Else
fullPath = doc.FullFileName
End If
Debug.Print Space(i); " - " + fullPath
Call PrintOccurrences( _
occ.SubOccurrences, _
i + 2)
Next
End Sub
Sub PrintStructure()
Dim doc As AssemblyDocument
Set doc = ThisApplication.ActiveDocument
Dim acd As AssemblyComponentDefinition
Set acd = doc.ComponentDefinition
Call PrintOccurrences(acd.Occurrences, 1)
End Sub

I get this output in case of my assembly that is referencing a SolidWorks assembly file called gears.SLDASM:

Solidworksassembly

 gears:1
- C:SolidWorksGearsgears.SLDASM
frame:1
- C:SolidWorksGearsframe.SLDPRT
Internal Spur Gear:1
- C:SolidWorksGearsInternal Spur Gear.sldprt
Spur Gear:1
- C:SolidWorksGearsSpur Gear.sldprt
Spur Gear:2
- C:SolidWorksGearsSpur Gear.sldprt
Spur Gear:3
- C:SolidWorksGearsSpur Gear.sldprt
Spur Gear:4
- C:SolidWorksGearsSpur Gear.sldprt
shaft1:1
- C:SolidWorksGearsshaft1.SLDPRT
shaft2:1
- C:SolidWorksGearsshaft2.SLDPRT
shaft2:2
- C:SolidWorksGearsshaft2.SLDPRT
shaft2:3
- C:SolidWorksGearsshaft2.SLDPRT

Comments

One response to “AnyCAD structure with original file names”

  1. Actcad 2022 software with Intellicad 10.1a engine

Leave a Reply to DeepikaCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading