Inventor API Controlling model space.

Inventor API Controlling model space.

garrettfulghum
Contributor Contributor
1,236 Views
9 Replies
Message 1 of 10

Inventor API Controlling model space.

garrettfulghum
Contributor
Contributor

So at this point, I am lost.

I've managed to create and control parts using a 3rd program written in c# and inventors API. However, now I can't seem to figure out how to constrain two objects. So here's my setup:

 

Basically, I'm pulling the occurrences in the assembly and getting their faces. Then using that, constraining two faces together. Ultimately no user interaction can happen during this process.

            ComponentOccurrences CompOccurrences;

            AssemblyComponentDefinition AsmCompDefinition;
            AsmCompDefinition = m_AssemblyDocument.ComponentDefinition;
            CompOccurrences = AsmCompDefinition.Occurrences;
            //grab occurrence 1
            ComponentOccurrence CompOccurrence = CompOccurrences[1];

            string OccurrenceName = CompOccurrence.Name;
            Console.WriteLine(OccurrenceName);

            object oFace1 = CompOccurrence.SurfaceBodies[1].Faces[6];
            //grab occurence two
            ComponentOccurrence CompOccurrence2 = CompOccurrences[2];

            object oFace2 = CompOccurrence2.SurfaceBodies[1].Faces[6];
            
            string OccurrenceName2 = CompOccurrence2.Name;
            Console.WriteLine(OccurrenceName2);

            double offset = 0;
            Console.ReadKey();
            Oconstraint.Constraints.AddMateConstraint(oFace1, oFace2, offset);

when I run this I get an exception on the Oconstraint.Constraints call. Any help would be greatly appreciated!

 

 

0 Likes
Accepted solutions (1)
1,237 Views
9 Replies
Replies (9)
Message 2 of 10

philip1009
Advisor
Advisor
Accepted solution

When constraining geometry in parts, you have to Create a Geometry Proxy first, then you use those proxies to generate constraints.  The only time you don't have to use a proxy is if you're constraining to a work feature at the assembly level like a work plane.

 

Here's the sample from the API guide, unfortunately it's in VBA, and I don't know how to translate to C+ yet:

Add mate constraint using work planes in parts

Public Sub MateConstraintOfWorkPlanes()
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

    ' Get references to the two occurrences to constrain.
    ' This arbitrarily gets the first and second occurrence.
    Dim oOcc1 As ComponentOccurrence
    Set oOcc1 = oAsmCompDef.Occurrences.Item(1)

    Dim oOcc2 As ComponentOccurrence
    Set oOcc2 = oAsmCompDef.Occurrences.Item(2)

    ' Get the XY plane from each occurrence.  This goes to the
    ' component definition of the part to get this information.
    ' This is the same as accessing the part document directly.
    ' The work plane obtained is in the context of the part,
    ' not the assembly.
    Dim oPartPlane1 As WorkPlane
    Set oPartPlane1 = oOcc1.Definition.WorkPlanes.Item(3)

    Dim oPartPlane2 As WorkPlane
    Set oPartPlane2 = oOcc2.Definition.WorkPlanes.Item(3)

    ' Because we need the work plane in the context of the assembly
    ' we need to create proxies for the work planes.  The proxies
    ' represent the work planes in the context of the assembly.
    Dim oAsmPlane1 As WorkPlaneProxy
    Call oOcc1.CreateGeometryProxy(oPartPlane1, oAsmPlane1)

    Dim oAsmPlane2 As WorkPlaneProxy
    Call oOcc2.CreateGeometryProxy(oPartPlane2, oAsmPlane2)

    ' Create the constraint using the work plane proxies.
    Call oAsmCompDef.Constraints.AddMateConstraint(oAsmPlane1, oAsmPlane2, 0)
End Sub
0 Likes
Message 3 of 10

garrettfulghum
Contributor
Contributor

Thanks for your help! I'll give it a try tomorrow. On that behalf, most of the API help is in VB. Would you ultimately recommend using VB instead of C+?

0 Likes
Message 4 of 10

philip1009
Advisor
Advisor

I would, iLogic only uses VB for example, but I would say I'm a little biased here since I've only focused on learning VB.

0 Likes
Message 5 of 10

garrettfulghum
Contributor
Contributor

Okay, I think Ill just use VB from now on. Its just safer to use since all the documentation is in VB. Ill still use Visual studio to write in.

 

Your answer works for me! Thanks! Is there anyway to constrain using Faces of the model aswell? Ive been reading up on the B-Rep and face detection and was able to detect faces and edges in C#. I just couldnt figure out how to constrain them together. 

 

again, thanks for the help.

0 Likes
Message 6 of 10

philip1009
Advisor
Advisor

In Visual Studio, I'm not sure how to differentiate between Faces unless you know how to check against other geometry and work features.  In the past, I always used work planes, axes, and points to make constraints by code since it's easy to call those features up by name.

 

Now in 2019, there's a function to be able to name Faces, Edges, and Vertexes to be used by a bunch of new snippets in iLogic.  I've slowly been updating my templates to take advantage of these new functions since it doesn't require more than one line of code to make a constraint, and it cleans up the models by removing unnecessary work features:

https://knowledge.autodesk.com/support/inventor-products/learn-explore/caas/CloudHelp/cloudhelp/2019...

 

I don't know if it's possible to call those geometry names in Visual Studio, I rarely venture outside of iLogic for all of my coding needs.

Message 7 of 10

garrettfulghum
Contributor
Contributor

Interesting,

My first couple of big projects were written in Ilogic and it was great. I just want the functionality of the user forms from visual studio. I know Inventors programming studio has some controls, but nowhere near the amount visual studio has. In the long run whats probably going to happen also, is that the program is going to be connected to a database to generate parameters and constraints. Something I dont think Ilogic can do.

 

At this point im at a cross road. Using Visual studio to connect to Inventor is way more difficult to work around in than Inventors studio. Plus all documentation is meant to be used as Ilogic/Built in. Not in a 3rd party application.

0 Likes
Message 8 of 10

philip1009
Advisor
Advisor

I disagree on the documentation, it's meant to be used for both Visual Studio and iLogic, there's just pros and cons for each method.  Visual Studio obviously is harder to start with and you're limited to methods in the exposed API where as iLogic has built-in snippets and bunch of behind the scenes stuff that makes creating and editing code easier for those starting out.  However iLogic is severely limited compared to full VB, the forms are barely adaptive even after you learn a few tricks, and a lot of VB functionality such as WithEvents and other functions I'm probably not aware of are not possible in iLogic, or you have to use some form of work-around to get the same functionality.  Visual Studio gives a lot of functionality with more complex forms and connectivity to other software API or even developing external software that uses Inventor along with other programs.  VS also gives you the ability to develop Add-Ins for Inventor to develop extra Inventor tools specific for your needs.  One of my big projects in the future will be to develop a company specific program to automate processes and transfer data between Inventor, Vault, Radan, and Infor Visual.

Message 9 of 10

garrettfulghum
Contributor
Contributor

Okay! So after playing around with things I think im going to use both C# and VB. Ill have all the inventor stuff in VB a class library I can call through C# with my main application. It actually makes everything so simple. Cant believe I didn't think of it earlier! Thanks for your help and for bouncing ideas back and forth. It helped a ton!

Message 10 of 10

R.Mabery
Advocate
Advocate

The ability to add the Named Geometry can be done thru the API by using Attributes.  In essence, that is what the named geometry is.

 

Take a look at Attributes in the API Help, read this article by Brian Ekins, and take a look at this Attribute Helper he wrote.

 

Attributes are amazing.  That is all.  🙂

 


Thanks,
Randy Mabery
Applications Expert
IMAGINiT Technologies