[VBA] Identify Parts and their location in Space

[VBA] Identify Parts and their location in Space

Anonymous
Not applicable
1,068 Views
4 Replies
Message 1 of 5

[VBA] Identify Parts and their location in Space

Anonymous
Not applicable

Is it possible to access part/member properties in an Inventor File?  Where is this data located?  How would one go to access it?

 

My goal is to extract the locaton a member starts, the location a member ends, and the name of a member.

 

Any material is appriciated.

 

-lemensk

0 Likes
Accepted solutions (1)
1,069 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
 
'--------------------------------
 
Dim oAssDoc As AssemblyDocument
oAssDoc = ThisApplication.ActiveDocument
Dim oConstraint As AssemblyConstraint
 
RUSure = MessageBox.Show _
("Are you sure you want to Delete all sick constraints?",  _
"iLogic",MessageBoxButtons.YesNo)
 
If RUSure = vbNo Then
Return
Else
          i = 0
          For Each oConstraint In oAssDoc.ComponentDefinition.Constraints
            If oConstraint.HealthStatus <> oConstraint.HealthStatus.kUpToDateHealth And _
            oConstraint.HealthStatus <> oConstraint.HealthStatus.kSuppressedHealth Then
          oConstraint.Delete
            i = i + 1
          End If
          Next
End If
MessageBox.Show(" A total of "i & " constraints were deleted.", "iLogic")

'---------------------------------------------

I thought that it would make sense that the information I am looking for would be located  in "oAssDoc.ComponentDefinition" but I was unable to find properties concerning location a parts location and the name of the part itself.
 
Does this illlustrate my goal better?
0 Likes
Message 3 of 5

MegaJerk
Collaborator
Collaborator

Actually, your original hunch was more correct than you may think! The spatial placement of objects is relevant to the origin of your assembly, so by looking at each component’s RangeBox (Component.ComponentDefinition.RangeBox. MinPoint/MaxPoint), we can get an idea about where that occurrence sits inside of our assembly. In addition,  using the usual formula

((MaxPoint.X – MinPoint.X) / 2.54) 

 

 



You can get the actual extents of the model in its current solid / bent shape.

Sadly there are some problems with this depending on, from what I can tell, the types of parts that you’re using in your assembly.  For solids, I’ve had better success with the RangeBox returning XYZ values that are correct, and will play nice with the cm to in conversion. However, with Sheet Metal, I find that the RangeBox returned is typically rather sloppy and does not equate to the correct values when math is worked on it (At least for MY sheet metal parts). No matter, you may be able to simply query the SurfaceBody of the Component Occurrence, check to see if the RangeBox is any different there, or even from the SurfaceBody, grab the FaceShell, and start iterating through the edges in order to come up with a Min / Max value for X,Y, & Z (which can then be used to equate a better bounding box.

I hope that this helps you on your way to pragmatically finding parts. Because I am at work, I have no code samples for you, however please feel free to ask if you have any further questions!

Thank you!



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 4 of 5

Anonymous
Not applicable

THANKS!  I'm still playing around with the code, but I beleive I found the data I've been looking for.  My desire is to convert an inventor model to RISA-3D for FEM analysis.  This is what I have so far (Sorry if it doesn't look pretty):

 

'CODE START----------------

Sub PrintPoints()


If ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
   Dim aDoc As Inventor.AssemblyDocument
   Set aDoc = ThisApplication.ActiveDocument
   Dim iPartOcc As Inventor.ComponentOccurrence
   Dim pDoc As Inventor.ComponentOccurrence
                
   Dim oWorkSurfaces As WorkSurfaces
   Dim oDataIO As DataIO
   Dim i As Integer
                
   Open "C:\Temp\result.txt" For Append As #1
                
   i = 0
   For Each iPartOcc In aDoc.ComponentDefinition.Occurrences    'Find the Part in Assembly
      For Each pDoc In iPartOcc.Definition.Occurrences
         If pDoc.Definition.SurfaceBodies.Count > 0 Then
            For Each pPoint In pDoc.SurfaceBodies(1).Vertices
               Dim strg As String
                  strg = pDoc.Name & ":" & pPoint.Point.X & " , " & pPoint.Point.Y & " , " & pPoint.Point.Z
                  Print #1, strg
                  i = i + 1
           Next

        End If
      Next
    Next
End If


 Close #1 ' Close file.

 

End Sub

 

'CODE END----------------

0 Likes
Message 5 of 5

Anonymous
Not applicable
Accepted solution

Below is the answer to my question.  Surprised how hard it was to get such simple code:

 

'CODE START ---------------------------------

Sub printParts()
   Dim inventDoc As Inventor.AssemblyDocument
   Dim deltaX, deltaY, deltaZ, avgX, avgY, avgZ, minX, minY, minZ, maxX, maxY, maxZ As Double
   Dim fileName, strg As String

   Dim partOccur As Inventor.ComponentOccurrence

   Set inventDoc = ThisApplication.ActiveDocument
   fileName = inventDoc.DisplayName

   For Each partOccur In inventDoc.ComponentDefinition.Occurrences.AllLeafOccurrences   'For each part included in the assembly
       strg = partOccur.Name & ";" & partOccur.RangeBox.minPoint.X & ";" & partOccur.RangeBox.minPoint.Y & _

                 ";" & partOccur.RangeBox.minPoint.Z


      Open "C:\Temp\" & fileName & ".txt" For Append As #1 'Open txt file and Append
      Print #1, strg                                                                         'Print String
      Close #1                                                                                'Close txt file
   Next
   
End Sub


 

'CODE STOP ---------------------------------

0 Likes