Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

intersect a sketch on one side of plane C++

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
alex_winter
453 Views, 5 Replies

intersect a sketch on one side of plane C++

alex_winter
Advocate
Advocate

Hello I have a part body and I want to create a intersectWithSketchPlane on the Z+ side of the xZ ConstructionPlane (origin is center of body) in C++. I have seen some examples in the Sample Programs but not trying to limit the output like this.

 

It is very similar to this post Projecting a face to a sketch only split half except for I am not using any split bodies and also using just intersectWithSketchPlane to do this.  

 

intersect.png

0 Likes

intersect a sketch on one side of plane C++

Hello I have a part body and I want to create a intersectWithSketchPlane on the Z+ side of the xZ ConstructionPlane (origin is center of body) in C++. I have seen some examples in the Sample Programs but not trying to limit the output like this.

 

It is very similar to this post Projecting a face to a sketch only split half except for I am not using any split bodies and also using just intersectWithSketchPlane to do this.  

 

intersect.png

5 REPLIES 5
Message 2 of 6
BrianEkins
in reply to: alex_winter

BrianEkins
Mentor
Mentor

Projecting geometry into a sketch will project the entire entity and can't be limited in any way. In your example, you're projecting the cylindrical face which will result in two sketch lines because the sketch intersects the cylinder in two places. My first idea was that you could split the body along the X-Z plane so you have a face that only exists on the positive side. This works but causes you to modify the body in a way that you probably would rather not.

 

The second idea is to do the intersection with the full cylinder and then delete the results you don't want. In this case, you would find the sketch line on the negative side and delete it.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes

Projecting geometry into a sketch will project the entire entity and can't be limited in any way. In your example, you're projecting the cylindrical face which will result in two sketch lines because the sketch intersects the cylinder in two places. My first idea was that you could split the body along the X-Z plane so you have a face that only exists on the positive side. This works but causes you to modify the body in a way that you probably would rather not.

 

The second idea is to do the intersection with the full cylinder and then delete the results you don't want. In this case, you would find the sketch line on the negative side and delete it.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 6
alex_winter
in reply to: BrianEkins

alex_winter
Advocate
Advocate

Thanks for replying.  Deleting the sketch lines on the negative side would work then.  I would rather not alter the body.   I can use an if statement to do this but not sure how to write it.   I can delete the sketch with the line below: 

skt1->deleteMe();

 

0 Likes

Thanks for replying.  Deleting the sketch lines on the negative side would work then.  I would rather not alter the body.   I can use an if statement to do this but not sure how to write it.   I can delete the sketch with the line below: 

skt1->deleteMe();

 

Message 4 of 6
BrianEkins
in reply to: alex_winter

BrianEkins
Mentor
Mentor

Here's a simple script that does what I suggested. I found a limitation with the API when intersecting a body with a sketch. It only supports intersecting a body. In the UI you can choose a body or individual entities, like a face. Using a body means you get more results when doing the intersection, but you can still delete them.

 

extern "C" XI_EXPORT bool run(const char* context)
{
	app = Application::get();
	if (!app)
		return false;

	ui = app->userInterface();
	if (!ui)
		return false;

	Ptr<Design> des = app->activeProduct();

	// Get the body from the root component. This is assuming
	// the first, and possibly only, body is the cylinder.
	Ptr<Component> root = des->rootComponent();
	Ptr<BRepBody> body = root->bRepBodies()->item(0);
	
	// Create a sketch on the X-Z plane.
	Ptr<Sketch> sketch = root->sketches()->add(root->xZConstructionPlane());

	// Intersect the body with the sketch plane.
	Ptr<ObjectCollection> results = sketch->projectCutEdges(body);

	// Find and delete all lines that are in the negative space.
	for (Ptr<SketchEntity> skEnt : results)
	{
		// Check if it's a sketch line.
		if (skEnt->objectType() == adsk::fusion::SketchLine::classType())
		{
			Ptr<SketchLine> skLine = skEnt;

			if (skLine->startSketchPoint()->geometry()->x() < 0.0 ||
				skLine->endSketchPoint()->geometry()->x() < 0.0)
			{
				skLine->deleteMe();
			}
		}
	}

	return true;
}
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes

Here's a simple script that does what I suggested. I found a limitation with the API when intersecting a body with a sketch. It only supports intersecting a body. In the UI you can choose a body or individual entities, like a face. Using a body means you get more results when doing the intersection, but you can still delete them.

 

extern "C" XI_EXPORT bool run(const char* context)
{
	app = Application::get();
	if (!app)
		return false;

	ui = app->userInterface();
	if (!ui)
		return false;

	Ptr<Design> des = app->activeProduct();

	// Get the body from the root component. This is assuming
	// the first, and possibly only, body is the cylinder.
	Ptr<Component> root = des->rootComponent();
	Ptr<BRepBody> body = root->bRepBodies()->item(0);
	
	// Create a sketch on the X-Z plane.
	Ptr<Sketch> sketch = root->sketches()->add(root->xZConstructionPlane());

	// Intersect the body with the sketch plane.
	Ptr<ObjectCollection> results = sketch->projectCutEdges(body);

	// Find and delete all lines that are in the negative space.
	for (Ptr<SketchEntity> skEnt : results)
	{
		// Check if it's a sketch line.
		if (skEnt->objectType() == adsk::fusion::SketchLine::classType())
		{
			Ptr<SketchLine> skLine = skEnt;

			if (skLine->startSketchPoint()->geometry()->x() < 0.0 ||
				skLine->endSketchPoint()->geometry()->x() < 0.0)
			{
				skLine->deleteMe();
			}
		}
	}

	return true;
}
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 5 of 6
alex_winter
in reply to: BrianEkins

alex_winter
Advocate
Advocate

Thanks Brian, this worked mostly except for there are some Curves that never got removed.  Looks like it's because it is removing only SketchLines so I changed it to SketchCurves but there is no startSketchPoint & endSketchPoint for that. 

0 Likes

Thanks Brian, this worked mostly except for there are some Curves that never got removed.  Looks like it's because it is removing only SketchLines so I changed it to SketchCurves but there is no startSketchPoint & endSketchPoint for that. 

Message 6 of 6
BrianEkins
in reply to: alex_winter

BrianEkins
Mentor
Mentor
Accepted solution

You're correct that my initial code only handles lines. Here's a more generic sample that should handle everything.

 

extern "C" XI_EXPORT bool run(const char* context)
{
	app = Application::get();
	if (!app)
		return false;

	ui = app->userInterface();
	if (!ui)
		return false;

	Ptr<Design> des = app->activeProduct();

	// Get the body from the root component. This is assuming
	// the first, and possibly only, body is the cylinder.
	Ptr<Component> root = des->rootComponent();
	Ptr<BRepBody> body = root->bRepBodies()->item(0);
	
	// Create a sketch on the X-Z plane.
	Ptr<Sketch> sketch = root->sketches()->add(root->xZConstructionPlane());

	// Intersect the body with the sketch plane.
	Ptr<ObjectCollection> results = sketch->projectCutEdges(body);

	// Find and delete all lines that are in the negative space.
	double tolerance = app->pointTolerance();
	for (Ptr<SketchEntity> skEnt : results)
	{
		Ptr<BoundingBox3D> bounds = skEnt->boundingBox();
		if (bounds->minPoint()->x() < -tolerance)
		{
			skEnt->deleteMe();
		}
	}

	return true;
}

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes

You're correct that my initial code only handles lines. Here's a more generic sample that should handle everything.

 

extern "C" XI_EXPORT bool run(const char* context)
{
	app = Application::get();
	if (!app)
		return false;

	ui = app->userInterface();
	if (!ui)
		return false;

	Ptr<Design> des = app->activeProduct();

	// Get the body from the root component. This is assuming
	// the first, and possibly only, body is the cylinder.
	Ptr<Component> root = des->rootComponent();
	Ptr<BRepBody> body = root->bRepBodies()->item(0);
	
	// Create a sketch on the X-Z plane.
	Ptr<Sketch> sketch = root->sketches()->add(root->xZConstructionPlane());

	// Intersect the body with the sketch plane.
	Ptr<ObjectCollection> results = sketch->projectCutEdges(body);

	// Find and delete all lines that are in the negative space.
	double tolerance = app->pointTolerance();
	for (Ptr<SketchEntity> skEnt : results)
	{
		Ptr<BoundingBox3D> bounds = skEnt->boundingBox();
		if (bounds->minPoint()->x() < -tolerance)
		{
			skEnt->deleteMe();
		}
	}

	return true;
}

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report