Coincident constraint on a part of an assembly using API with C#

Coincident constraint on a part of an assembly using API with C#

nagihan.bostan
Enthusiast Enthusiast
282 Views
2 Replies
Message 1 of 3

Coincident constraint on a part of an assembly using API with C#

nagihan.bostan
Enthusiast
Enthusiast

Hi,
I did research on Geometric Constraints.AddCoincident in the form, but I couldn't find a solution to my problem.
- I select a part in assembly.
- I choose two planes ( A and B Plane )that are perpendicular to each other from selected part.
- I create a point using AddByProjectingEntity on A plane and I create a line on B plane.


I want to carry the line to the point where I created it. For this I use GeometricConstraints.AddCoincident. But the line didn't join with the point, as I did in Inventor.
Thanks in advance.

 

My Code:

private void AddCoincident()
{


Inventor.Application application = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");

AssemblyDocument oAsmDoc = (AssemblyDocument)application.ActiveDocument;
TransientGeometry oTG = application.TransientGeometry;


ComponentOccurrence componentOccurrence = application.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select a Part");
PartComponentDefinition oCompDef = (PartComponentDefinition)componentOccurrence.Definition;


WorkPlane Face1 = (WorkPlane)application.CommandManager.Pick(SelectionFilterEnum.kWorkPlaneFilter, "Select Face1");

PlanarSketch sktch = oCompDef.Sketches.Add(Face1, true);

SketchPoint oSketchCenterPoint = (SketchPoint)sktch.AddByProjectingEntity(oCompDef.WorkPoints["Center Point"]);

//double Cons = oSketchCenterPoint.Geometry3d.X;

WorkPlane Face2 = (WorkPlane)application.CommandManager.Pick(SelectionFilterEnum.kWorkPlaneFilter, "Select Face2");

PlanarSketch sktch1 = oCompDef.Sketches.Add(Face2, true);


Point2d P1 = oTG.CreatePoint2d(0, 0);

Point2d P2 = oTG.CreatePoint2d(5, 0);

SketchLine line = sktch1.SketchLines.AddByTwoPoints(P1, P2);

//pipe.EndSketchPoint.Merge(oSketchCenterPoint1);
sktch1.Edit();

sktch1.GeometricConstraints.AddCoincident(line.StartSketchPoint as SketchEntity, oSketchCenterPoint as SketchEntity);


}

AppForm1.PNG

 

AppForm2.PNG

 

0 Likes
Accepted solutions (1)
283 Views
2 Replies
Replies (2)
Message 2 of 3

Michael.Navara
Advisor
Advisor
Accepted solution

You can NOT create constraint between geometries in two different sketches. You need to project oSketchCenterPoint to sktch1 and this projected (newly created) point you can Merge with line.StartSketchPoint

Message 3 of 3

nagihan.bostan
Enthusiast
Enthusiast

Thanks for the idea. I edited my code as below and the code works correctly now.

 

Code:

 


Inventor.Application application = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");

AssemblyDocument oAsmDoc = (AssemblyDocument)application.ActiveDocument;
TransientGeometry oTG = application.TransientGeometry;


ComponentOccurrence componentOccurrence = application.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select a Part");
PartComponentDefinition oCompDef = (PartComponentDefinition)componentOccurrence.Definition;


WorkPlane Face1 = (WorkPlane)application.CommandManager.Pick(SelectionFilterEnum.kWorkPlaneFilter, "Select Face1");

PlanarSketch sktch = oCompDef.Sketches.Add(Face1, true);

SketchPoint oSketchCenterPoint = (SketchPoint)sktch.AddByProjectingEntity(oCompDef.WorkPoints["Center Point"]);

WorkPlane Face2 = (WorkPlane)application.CommandManager.Pick(SelectionFilterEnum.kWorkPlaneFilter, "Select Face2");

PlanarSketch sktch1 = oCompDef.Sketches.Add(Face2, true);

 

SketchPoint oSketchCenterPointNew = (SketchPoint)sktch1.AddByProjectingEntity(oSketchCenterPoint);

 

Point2d P1 = oTG.CreatePoint2d(0, 0);

Point2d P2 = oTG.CreatePoint2d(5, 0);

SketchLine line = sktch1.SketchLines.AddByTwoPoints(P1, P2);

sktch1.GeometricConstraints.AddCoincident(line.StartSketchPoint as SketchEntity, oSketchCenterPointNew as SketchEntity);

0 Likes