insert an instance of a component and constrain

insert an instance of a component and constrain

DWhiteley
Advisor Advisor
946 Views
9 Replies
Message 1 of 10

insert an instance of a component and constrain

DWhiteley
Advisor
Advisor

Guys,

 

I'm writing a bit of VBA to add another instance of an existing assembly component & constrain it.

The problem is that the edge of the original instance is being constrained and NOT the edge  of the new instance.

Notice that I added a constraint to the original component (perhaps there's an easier method?).

 

Here's the code:

 

If oRad1 = oRad Then
' take a copy of part and insert
Dim oOccurence As ComponentOccurrence
Set oOccurence = oOccurences.Add(oPath, Omatrix)

Set oDoc2 = oOccurence.Definition.Document
Set oEdge = oDoc2.AttributeManager.FindObjects("iSert")


Set oInsert = AssmDoc.ComponentDefinition.Constraints.AddInsertConstraint(oEdge, oEdgeH, True, 0)

Set oEdge = Nothing
Set oDoc2 = Nothing


End If

 

 

Can anyone see what's wrong?

 

Many thanks in advance.

 

Dave

Envisage UK Ltd

0 Likes
947 Views
9 Replies
Replies (9)
Message 2 of 10

ekinsb
Alumni
Alumni

You need to define the edge in the context of the assembly, not the part.  In other words, you need a full path to the edge.  As you said, you now have two of these parts in your assembly so if you just specify the edge in the part, how does Inventor know which edge you're referring to?  The "full path" to the edge means also specify which occurrence and all of that information is packaged in an EdgeProxy.  So the solution is to create a proxy to the edge.  The changes to your code are highlighted in red below.

 

If oRad1 = oRad Then
' take a copy of part and insert
Dim oOccurence As ComponentOccurrence
Set oOccurence = oOccurences.Add(oPath, Omatrix)

Set oDoc2 = oOccurence.Definition.Document
Set oEdge = oDoc2.AttributeManager.FindObjects("iSert")

 

Dim newEdge as EdgeProxy

call oOccurrence.CreateGeometryProxy(oEdge, newEdge)

Set oInsert = AssmDoc.ComponentDefinition.Constraints.AddInsertC​onstraint(newEdge, oEdgeH, True, 0)

Set oEdge = Nothing
Set oDoc2 = Nothing


End If

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 3 of 10

DWhiteley
Advisor
Advisor
Many thanks Brian - for your superb assistance (as normal).
0 Likes
Message 4 of 10

DWhiteley
Advisor
Advisor

Hi Brian,

 

I altered the code, but even though the extra instances are added to the assembly, the insert constraint does not work.

 

    Dim oFace As Face
      Dim oRad As Double
    Set oFace = ThisApplication.CommandManager.Pick( _
                        kPartFacePlanarFilter, _
                        "Select the face")
                        
   
                Dim oOccurences As ComponentOccurrences
        Set oOccurences = AssmDoc.ComponentDefinition.Occurrences
       
        ' Set a reference to the transient geometry object.
        Dim oTG As TransientGeometry
        Set oTG = ThisApplication.TransientGeometry
       
        Dim Omatrix As Matrix
        Set Omatrix = oTG.CreateMatrix
        Call Omatrix.SetToIdentity
                  
                  
    For i = 1 To oFace.Edges.Count
   
       
       
      Set oEdgeH = oFace.Edges(i)
      If oEdgeH.GeometryType = kCircleCurve Then
     
        Set ocir = oEdgeH.Geometry
        oRad = ocir.Radius
       
       
     
       
        If oRad1 = oRad Then
            ' take a copy of part and insert
            Dim oOccurence As ComponentOccurrence
            Set oOccurence = oOccurences.Add(oPath, Omatrix)
           
            Set oDoc2 = oOccurence.Definition.Document
            Set oEdge = oDoc2.AttributeManager.FindObjects("iSert")
           
            Dim newedge As EdgeProxy
            Call oOccurence.CreateGeometryProxy(oEdge, newedge)
           
                                  
            Set oInsert = AssmDoc.ComponentDefinition.Constraints.AddInsertConstraint(newedge, oEdgeH, True, 0)
           
            Set oEdge = Nothing
            Set oDoc2 = Nothing
           

       
        End If
       
     
      End If
     
     
     
    Next

0 Likes
Message 5 of 10

DWhiteley
Advisor
Advisor

Ahh!

 

It's failing here, that's why the constraint doesn't work.

 

Capture.GIF

 

The code to add the attibute is:

 

    Dim oEdge As Edge
    Set oEdge = ThisApplication.CommandManager.Pick( _
                        kPartEdgeCircularFilter, _
                        "Select the circular edge")
                       

       
    Dim oDoc1 As Document
    Set oDoc1 = oEdge.Parent.ComponentDefinition.Document
   
    Dim oPath As String
    oPath = oDoc1.FullFileName
   
    Dim oAttribSets As AttributeSets
    Set oAttribSets = oDoc1.AttributeSets
   
    Dim oAttribset As AttributeSet
   
    On Error Resume Next
   
   
    Set oAttribset = oEdge.AttributeSets.Add("iSert")
    
      
    Call oDoc1.Update

 

 

0 Likes
Message 6 of 10

ekinsb
Alumni
Alumni

It's difficult to tell exactly what's going on with the information you've provided.  An attribute to the edge can exist in the part document, but it can also exist in the assembly, where it would actually be an attribute to the edge proxy.  I assume you're creating the attribute within the part.  oDoc2 is being set to the part document and querying for the attribute there, so it should find it.  The edge you'll get back will be in the context of the part so a proxy will need to be constructed to use it in the assembly, but you have the code to do that too so it looks like it should be working.

 

Attributes can sometimes be tricky to work with because it's hard to see what's actually going on.  If you don't have it already I recommend getting the Attribute Manager utility.  You can get it here: http://modthemachine.typepad.com/my_weblog/2015/08/attribute-helper-update.html

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 7 of 10

DWhiteley
Advisor
Advisor

Many thanks Brian,

 

That's a neat utility, that others on this forum could make good use of!

Let me ask you this.

Am I going down the correct route here?

I'm trying to write the age old  routine "copy parts/bolts to the same size holes".

 

So I've written the code where I select a circular edge of a bolt, then a circular edge of a hole. These then get assembled together.

However, when I select a face and retrieve the circluar edges of the same size, I don't know where to go with assembling/copying the part/bolt to the other holes.

 

I hope I've axplained this right??

 

I thought that using attributes would work - or can you reccomend another method that I should investigate?

 

Many thanks in advance,

 

Dave

Envisage UK Ltd

0 Likes
Message 8 of 10

ekinsb
Alumni
Alumni

I think you're on the right track.  There's one thing that you might be getting hung up on though.  If you have the user pick a face of a part while he's in the assembly, what's returned from the selection is a FaceProxy.  If you iterate over the edges of that face, EdgeProxy objects are returned so you don't need to create a proxy for those.  You're code looks correct where you're inserting the bolt and then going to the bolt document to query for attributes to find the edge of the bolt.  The attribute should exist in the part and so you need to do the query there.  But what's returned is the edge in the part so you'll need to create a proxy for the edge to be able to use it in the assembly.

 

If you still have problems, if you can post a simple test case of code and part and assembly files that can be used to reproduce it I can take a look to see where you're getting hung up.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 9 of 10

DWhiteley
Advisor
Advisor

Many thanks Brian,

 

I'll take another look at it. Thanks for your guidance on this.

 

PS And on another note,  congratulations on helping to make Fusion 360 a great product!

 

Dave

0 Likes
Message 10 of 10

dg2405
Advocate
Advocate

Hey,

i think this code will help you, maybe it's your finish solution 🙂

http://forums.autodesk.com/t5/inventor-customization/rivet-assembly/td-p/6286291/page/2

take the last posted code.

0 Likes