intersectWithSurface Does Not Work

intersectWithSurface Does Not Work

tperam
Advocate Advocate
892 Views
3 Replies
Message 1 of 4

intersectWithSurface Does Not Work

tperam
Advocate
Advocate
 
intersectWithSurface Does Not Work
 
What Fusion 360 Objects are the inner and outer Surfaces of Hollow Cylindrical or Conical and the like?
Are they BRepFaces or BRepEdges or Surface?
 
I am unable to implement the following. I have omitted the steps to create the hollow solid.
 
//////////////////////
Ptr<Vector3D> normal = adsk::core::Vector3D::create(0, 0, 1);
Ptr<Plane> IntersectPlane = adsk::core::Plane::create(adsk::core::Point3D::create(0, 0, 0), normal);
 
Ptr<ObjectCollection> IntersectionCurvesObjColl = adsk::core::ObjectCollection::create();
Ptr<Surface> surface[2]; /* Outer and Inner surfaces */
 
IntersectionCurvesObjColl = IntersectPlane->intersectWithSurface(surface[0]);
if (!IntersectionCurvesObjColl)
   return false;
 
/////////////////////////////
 
I could not find any sample program or steps in my searches in Fusion 360 API and Scripts Reference or the Fusion 360 API and Scripts Forum.
 
I will appreciate if someone could answer my question and send me a sample program or suitable steps to implement intersectWithSurface preferably using C++.
 
Regards,
Thurai
0 Likes
Accepted solutions (2)
893 Views
3 Replies
Replies (3)
Message 2 of 4

goyals
Autodesk
Autodesk
Accepted solution

I am getting only one face, cylindrical, in case of hollow cylinder. You can get the underlying geometry of face using geometry method on BrepFace object and can use it to find the intersection with plane. If possible please share the script in case you are seeing any problem. I just tried following script with a hollow cylinder and it works for me

app = adsk.core.Application.get()

root = app.activeProduct.rootComponent

bodies = root.bRepBodies

body = bodies[0]

faces = body.faces

face = faces[0]

geometry = face.geometry

normal = adsk.core.Vector3D.create(0, 0, 1)

plane = adsk.core.Plane.create(adsk.core.Point3D.create(0, 0, 0), normal)

IntersectionCurvesObjColl = plane.intersectWithSurface(geometry)



Shyam Goyal
Sr. Software Dev. Manager
Message 3 of 4

tperam
Advocate
Advocate
Accepted solution

Hi Goyals,

                Thanks for responding to my issue and my message.

 

It worked Okay. 

 

The C++ Code is below.

 

Regards,

Thurai

 

///////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////
#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <CAM/CAMAll.h>
using namespace adsk::core;
using namespace adsk::fusion;
using namespace adsk::cam;
using namespace std;
Ptr<Application> app;
Ptr<UserInterface> ui;

extern "C" XI_EXPORT bool run(const char* context)
{
 Ptr<Application> app = Application::get();
 if (!app)
  return false;
 ui = app->userInterface();
 if (!ui)
  return false;
 Ptr<Documents> documents = app->documents();
 if (!documents)
  return false;
 Ptr<Document> doc = documents->add(DocumentTypes::FusionDesignDocumentType);
 if (!doc)
  return false;
 Ptr<Product> product = app->activeProduct();
 if (!product)
  return false;
 Ptr<Design> design = product;
 if (!design)
  return false;
 // Get the root component of the active design.
 Ptr<Component> rootComp = design->rootComponent();
 if(!rootComp)
  return false;
 // Create a new sketch on the xy plane.
 Ptr<Sketches> sketches = rootComp->sketches();
 if (!sketches)
  return false;
 Ptr<ConstructionPlane> xyPlane = rootComp->xYConstructionPlane();
 if (!xyPlane)
  return false;
 Ptr<Sketch> sketch = sketches->add(xyPlane);
 if (!sketch)
  return false;
 // Draw two connected lines.
 Ptr<SketchCurves> curves = sketch->sketchCurves();
 if (!curves)
  return false;

 char dStr[16];
    string msg;
//////////////////////////////////////////////
 Ptr<SketchCurves> sketchCurves = sketch->sketchCurves();
 if(!sketchCurves)
  return false;
 Ptr<SketchCircles> sketchCircles = sketchCurves->sketchCircles();
 if(!sketchCircles)
  return false;
 Ptr<Point3D> centerPoint = Point3D::create(0, 0, 0);
 if(!centerPoint)
  return false;
 Ptr<SketchCircle> circle = sketchCircles->addByCenterRadius(centerPoint, 5.0);
 if(!circle)
  return false;
 Ptr<SketchCircle> circle2 = sketchCircles->addByCenterRadius(centerPoint, 3.0);
 if(!circle)
  return false;
 // Get extrude features
 Ptr<Features> feats = rootComp->features();
 if (!feats)
  return false;
 Ptr<ExtrudeFeatures> extrudes = feats->extrudeFeatures();
 if (!extrudes)
  return false;
 // Get the profile defined by the circle
 Ptr<Profiles> profs = sketch->profiles();
 if(!profs)
  return false;
 Ptr<Profile> prof = profs->item(0);
 if(!prof)
  return false;
 Ptr<ValueInput> distance = ValueInput::createByReal(5);
 if (!distance)
  return false;
 Ptr<ExtrudeFeature> extrude1 = extrudes->addSimple(prof, distance, adsk::fusion::FeatureOperations::NewBodyFeatureOperation);
 if (!extrude1)
  return false;
 // Get the body created by the extrusion
 Ptr<BRepBodies> bodies = extrude1->bodies();
 if (!bodies)
  return false;
 Ptr<BRepBody> body = bodies->item(0);
 if (!body)
  return false;
////////////////////////////////////////////////////////
 
 Ptr<Vector3D> normal = adsk::core::Vector3D::create(0, 0, 1);
 if(!normal)
 return false;
 Ptr<Plane> xyIntersectPlane = adsk::core::Plane::create(adsk::core::Point3D::create(0, 0, 2), normal);
 if (!xyIntersectPlane)
  return false;

 
 Ptr<ObjectCollection> IntersectionObjColl = adsk::core::ObjectCollection::create();
 if(!IntersectionObjColl)
  return false;
 ui->messageBox(" After IntersectionPtsObjColl ");

    Ptr<BRepFaces> faces = body->faces();
 
 ui->messageBox(" After faces ");
 unsigned int faceCount = faces->count();
  msg = " faceCount ";
  sprintf_s (dStr, "%d", faceCount); // %d makes the result be a decimal integer
  msg += dStr;
  msg += " ";
  ui->messageBox(msg);
 // 4 Faces: Front, Rear, Outer Curved, Inner Curved
 Ptr<BRepFace> face[4];
 
 /* Get the Inner and Outer faces */
 face[0] = faces->item(0);
 if(!face[0])
  return false;
 ui->messageBox(" After face[0] ");
 // Get the value of the property.
    Ptr<Surface> surface[4];
 
 surface[0] = face[0]->geometry();
 if(!surface[0])
  return false;
 ui->messageBox(" After surface[0] ");
 
 IntersectionObjColl = xyIntersectPlane->intersectWithSurface(surface[0]);
 if (!IntersectionObjColl)
  goto Face1;
 ui->messageBox(" After intersectWithSurface() face 0 ");

/////////////////////////////////////////////////////////////////////
Face1:;
 face[1] = faces->item(1);
 if(!face[1])
  return false;
 ui->messageBox(" After face[1] ");
 surface[1] = face[1]->geometry();
 if(!surface[1])
  return false;
 ui->messageBox(" After surface[1] ");
  IntersectionObjColl = xyIntersectPlane->intersectWithSurface(surface[1]);
 if (!IntersectionObjColl)
  goto Face2;
 ui->messageBox(" After intersectWithSurface() face 1 ");

/////////////////////////////////////////////////////
Face2:;
 face[2] = faces->item(2);
 if(!face[2])
  return false;
 ui->messageBox(" After face[2] ");
 surface[2] = face[2]->geometry();
 if(!surface[2])
  return false;
 ui->messageBox(" After surface[2] ");
 IntersectionObjColl = xyIntersectPlane->intersectWithSurface(surface[2]);
 if (!IntersectionObjColl)
  goto Face3;
 ui->messageBox(" After intersectWithSurface() face 3 ");

//////////////////////////////////////////////////////////////////
Face3:;
 face[3] = faces->item(3);
 if(!face[3])
  return false;
 ui->messageBox(" After face[3] ");
 surface[3] = face[3]->geometry();
 if(!surface[3])
  return false;
 ui->messageBox(" After surface[3] ");
 IntersectionObjColl = xyIntersectPlane->intersectWithSurface(surface[3]);
 if (!IntersectionObjColl)
  return false;
 ui->messageBox(" After intersectWithSurface() face 3 ");

 return true;
}
#ifdef XI_WIN
#include <windows.h>
BOOL APIENTRY DllMain(HMODULE hmodule, DWORD reason, LPVOID reserved)
{
 switch (reason)
 {
 case DLL_PROCESS_ATTACH:
 case DLL_THREAD_ATTACH:
 case DLL_THREAD_DETACH:
 case DLL_PROCESS_DETACH:
  break;
 }
 return TRUE;
}
#endif // XI_WIN

 

 

0 Likes
Message 4 of 4

tperam
Advocate
Advocate

Hi Goyals,

                Here is the complete C++ Code which takes IntersectWithSurface() to the next level of determining the  Curve 3D as item(0) in the Object Colloection, then Use IntersectWithCurve() to find the Intersection Points. 

 

The following C++ code worked Okay.

 

Regards,

Thurai

 

///////////////////////////////////////////////////////////////////////////////////

#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <CAM/CAMAll.h>

using namespace adsk::core;
using namespace adsk::fusion;
using namespace adsk::cam;
using namespace std;

Ptr<Application> app;
Ptr<UserInterface> ui;


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

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

Ptr<Documents> docs = app->documents();
if (!docs)
return false;

// Create a document.
Ptr<Document> doc = docs->add(DocumentTypes::FusionDesignDocumentType);
if (!doc)
return false;

Ptr<Design> design = app->activeProduct();
if (!design)
return false;

Ptr<Component> rootComp = design->rootComponent();
if (!rootComp)
return false;

char dStr[16];
string msg;

// Create profile 1
Ptr<Sketches> sketches = rootComp->sketches();
if (!sketches)
return false;

Ptr<Sketch> sketch0 = sketches->add(rootComp->xYConstructionPlane());
if (!sketch0)
return false;


Ptr<SketchCurves> sketchCurves = sketch0->sketchCurves();
if (!sketchCurves)
return false;

Ptr<SketchLines> sketchLines = sketchCurves->sketchLines();
if (!sketchLines)
return false;


Ptr<SketchArcs> sketchArcs = sketchCurves->sketchArcs();
if (!sketchArcs)
return false;


Ptr<Point3D> Point00 = adsk::core::Point3D::create(5, -5, -10);
if (!Point00)
return false;

Ptr<Point3D> Point01 = adsk::core::Point3D::create(8, -5, 0);
if (!Point01)
return false;


Ptr<Point3D> Point02 = adsk::core::Point3D::create(5, -5, 10);
if (!Point02)
return false;

Ptr<SketchLine> Line00 = sketchLines->addByTwoPoints(Point00, Point01);

Ptr<SketchLine> Line01 = sketchLines->addByTwoPoints(Point01, Point02);

Ptr<SketchArc> arc0 = sketchArcs->addFillet(Line00, Line00->startSketchPoint()->geometry(), Line01, Line01->endSketchPoint()->geometry(), 5.0);
if (!arc0)
return false;

ui->messageBox("After Fillet arc0 ");


Ptr<Point3D> Point10 = adsk::core::Point3D::create(5, 5, -15);
if (!Point10)
return false;


Ptr<Point3D> Point11 = adsk::core::Point3D::create(10, 5, 0);
if (!Point11)
return false;

Ptr<Point3D> Point12 = adsk::core::Point3D::create(5, 5, 15);
if (!Point12)
return false;


Ptr<SketchLine> Line10 = sketchLines->addByTwoPoints(Point10, Point11);

Ptr<SketchLine> Line11 = sketchLines->addByTwoPoints(Point11, Point12);

Ptr<SketchArc> arc1 = sketchArcs->addFillet(Line10, Line10->startSketchPoint()->geometry(), Line11, Line11->endSketchPoint()->geometry(), 8.0);
if (!arc1)
return false;

ui->messageBox("After Fillet arc1 ");

// Create loft feature input
Ptr<Features> features = rootComp->features();
if (!features)
return false;

Ptr<LoftFeatures> loftFeatures = features->loftFeatures();
if (!loftFeatures)
return false;

ui->messageBox("After loftFeatures ");

Ptr <Path> OpenProfile0 = adsk::fusion::Path::create(Line00, connectedChainedCurves);

Ptr <Path> OpenProfile1 = adsk::fusion::Path::create(Line10, connectedChainedCurves);

ui->messageBox("After OpenProfile1 ");

Ptr<LoftFeatureInput> loftInput = loftFeatures->createInput(adsk::fusion::FeatureOperations::NewBodyFeatureOperation);
if (!loftInput)
return false;

Ptr<LoftSections> loftSections = loftInput->loftSections();
if (!loftSections)
return false;

Ptr<LoftSection> loftSection0 = loftSections->add(OpenProfile0);
if (!loftSection0)
return false;

ui->messageBox("After loftSection0");

Ptr<LoftSection> loftSection1 = loftSections->add(OpenProfile1);
if (!loftSection1)
return false;

ui->messageBox("After loftSection1");

loftInput->isSolid(true);


// Create loft feature
Ptr<LoftFeature> loftFeature = loftFeatures->add(loftInput);
if (!loftFeature)
return false;

ui->messageBox("After Loft");

// Fit to window
Ptr<Viewport> viewPort = app->activeViewport();
if(!viewPort)
return false;
Ptr<Camera> cam = viewPort->camera();
if(!cam)
return false;
cam->isFitView(true);
viewPort->camera(cam);


// Get the body created by the extrusion
Ptr<BRepBodies> bodies = loftFeature->bodies();
if (!bodies)
return false;

Ptr<BRepBody> body = bodies->item(0);
if (!body)
return false;

ui->messageBox("After body ");


Ptr<Vector3D> normal = adsk::core::Vector3D::create(0, 0, 1);
if(!normal)
return false;

Ptr<Plane> xyIntersectPlane = adsk::core::Plane::create(adsk::core::Point3D::create(0, 0, 2), normal);
if (!xyIntersectPlane)
return false;


Ptr<ObjectCollection> IntersectionObjColl = adsk::core::ObjectCollection::create();
if(!IntersectionObjColl)
return false;

ui->messageBox(" After IntersectionPtsObjColl ");


Ptr<BRepFaces> faces = body->faces();

ui->messageBox(" After faces ");

unsigned int faceCount = faces->count();

msg = " faceCount ";
sprintf_s (dStr, "%d", faceCount); // %d makes the result be a decimal integer
msg += dStr;
msg += " ";
ui->messageBox(msg);

// 2 Faces: Outer Curved, Inner
Ptr<BRepFace> face[2];

/* Get the Inner and Outer faces */
face[0] = faces->item(0);
if(!face[0])
return false;

ui->messageBox(" After face[0] ");

// Get the value of the property.
Ptr<Surface> surface[4];

surface[0] = face[0]->geometry();
if(!surface[0])
return false;

ui->messageBox(" After surface[0] ");


IntersectionObjColl = xyIntersectPlane->intersectWithSurface(surface[0]);
if (!IntersectionObjColl)
// goto Face1;
return false;

ui->messageBox(" After intersectWithSurface() face 0 ");

unsigned int CountObjectsColl = IntersectionObjColl->count();

msg = " CountObjectsColl = ";
msg += " ";
sprintf_s (dStr, "%d", CountObjectsColl ); // %d makes the result be a decimal integer
msg += dStr;
msg += " ";
ui->messageBox(msg);

Ptr<Base> base = IntersectionObjColl->item(0);

msg = " base = ";
msg += " ";
sprintf_s (dStr, "%d", base );
msg += dStr;
msg += " ";
ui->messageBox(msg);

// Get the value of the property.
//string propertyValue = objectCollection_var->objectType();
string objectType = IntersectionObjColl->objectType();

msg = " objectType = ";
msg += " ";
sprintf_s (dStr, "%s", objectType );
msg += dStr;
msg += " ";
ui->messageBox(msg);


Ptr<Curve3D> curve3D = IntersectionObjColl->item(0);
if(!curve3D)
return false;

ui->messageBox(" After curve3D = IntersectionObjColl->item(0); ");

// Get the value of the property.
string CurveType = curve3D->objectType();

ui->messageBox(" After CurveType = sketchCurve->objectType(); ");

msg = " CurveType = ";
msg += " ";
sprintf_s (dStr, "%s", CurveType );
msg += dStr;
msg += " ";
ui->messageBox(msg);


/* Create an xz Plane with y Offset */
Ptr<Vector3D> normalxz = adsk::core::Vector3D::create(0, 1, 0);
if(!normalxz)
return false;

Ptr<Plane> xzPlane = adsk::core::Plane::create(adsk::core::Point3D::create(0, 2, 0), normalxz);
if (!xzPlane)
return false;

Ptr<Point3D> IntersectionPts[2];

/* Dummy Initialize */
IntersectionPts[0] = adsk::core::Point3D::create(0, 0, 0);
IntersectionPts[1] = adsk::core::Point3D::create(0, 0, 0);

Ptr<ObjectCollection> IntersectionPtsObjColl;


IntersectionPtsObjColl = adsk::core::ObjectCollection::create();
if(!IntersectionPtsObjColl)
return false;

IntersectionPtsObjColl = xzPlane->intersectWithCurve(curve3D);
if (!IntersectionPtsObjColl)
return false;


IntersectionPts[0] = IntersectionPtsObjColl->item(0);


ui->messageBox(" After IntersectionPts Line ");

double x, y, z;

x = IntersectionPts[0]->x();
y = IntersectionPts[0]->y();
z = IntersectionPts[0]->z();


msg = " x = ";
msg += " ";
sprintf_s ( dStr, "%f", x ); // %d makes the result be a decimal integer
msg += dStr;
msg += " ";
ui->messageBox(msg);

msg = " y = ";
msg += " ";
sprintf_s ( dStr, "%f", y ); // %d makes the result be a decimal integer
msg += dStr;
msg += " ";
ui->messageBox(msg);

msg = " z = ";
msg += " ";
sprintf_s ( dStr, "%f", z ); // %d makes the result be a decimal integer
msg += dStr;
msg += " ";
ui->messageBox(msg);


if(CurveType == "SketchLine")
ui->messageBox(" CurveType is SketchLine ");

else if(CurveType == "SketchArc")
ui->messageBox(" CurveType is SketchArc ");

if(CurveType == "Line3D")
ui->messageBox(" CurveType is SketchLine ");

else if(CurveType == "Arc3D")
ui->messageBox(" CurveType is SketchArc ");


else
ui->messageBox(" CurveType is None of the Above ");

return true;


}

#ifdef XI_WIN

#include <windows.h>

BOOL APIENTRY DllMain(HMODULE hmodule, DWORD reason, LPVOID reserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

#endif // XI_WIN

0 Likes