creating a Path from a Sketch that was projected on a surface always fail (C++)

creating a Path from a Sketch that was projected on a surface always fail (C++)

meeloo
Participant Participant
614 Views
3 Replies
Message 1 of 4

creating a Path from a Sketch that was projected on a surface always fail (C++)

meeloo
Participant
Participant

Hi,

 

I'm trying to create a Path from the sketch resulting of a call to projectToSurface.

 

auto fret_profile = component->sketches()->add(component->yZConstructionPlane());
auto profile = fret_profile->projectToSurface(faces, curves, AlongVectorSurfaceProjectType, component->zConstructionAxis())[0];
auto path = Path::create(profile, connectedChainedCurves);
auto input = component->features()->sweepFeatures()->createInput(fret_wire_profile, path, NewBodyFeatureOperation);

 

 

however the Path is always null and a call to getLastError gives me the following string:

InterValidationError : 

Utils::getObjectPath(obj.get(), objPath, nullptr, contextPath)

 

Please note that doing it in the application does work right away (I can create the sweep from the sketches my script creates).

 

I've tried many things to fix this and programatically create a valid Path from my projected sketch but came empty handed. The sketch I try to create the Path with is purple which I guess is because it's the product of the projectToSurface call.

 

Is there anything I can do to fix this?

 

Thanks!

 

 

0 Likes
615 Views
3 Replies
Replies (3)
Message 2 of 4

meeloo
Participant
Participant

I got it working by dynamically casting each sketchCurve and extracting the 3D nurbs from the SketchArc and SketchElipticalArc by hand. It's quite cumbersome and I'm hoping there is a better and simpler method...

0 Likes
Message 3 of 4

BrianEkinsADSK
Autodesk
Autodesk

It works for me.  Attached is the model I tested with and below is an example of the result and the code.

ProjectSketch.png

void ProjectTest()
{
	app = Application::get();
	ui = app->userInterface();

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

    // Have the sketch selected and package the curves into an array.
    Ptr<Selection> selection = ui->selectEntity("Select the sketch to Project", "Sketches");
    Ptr<Sketch> skToProj = selection->entity();
    Ptr<SketchCurves> skCurves = skToProj->sketchCurves();
    std::vector<Ptr<Base>> curves(skCurves->count());
    for (int i=0; i < skCurves->count(); ++i)
    {
        curves[i] = skCurves->item(i);
    }

    // Have the face selected and packet it into an array.
    selection = ui->selectEntity("Select the face to project to", "Faces");
    Ptr<BRepFace> faceTarget = selection->entity();
    std::vector<Ptr<BRepFace>> faces(1);
    faces[0] = faceTarget;

    // Create a new sketch to contain the projected curve.
    Ptr<Sketch> newSketch = root->sketches()->add(root->xYConstructionPlane());

    // Project the curves onto the face.
    std::vector<Ptr<SketchEntity>> result;
    result = newSketch->projectToSurface(faces, curves, SurfaceProjectTypes::AlongVectorSurfaceProjectType, root->zConstructionAxis());

    // Package the results into an ObjectCollection.
    Ptr<ObjectCollection> curveColl = ObjectCollection::create();
    for (Ptr<SketchCurve> curve : result)
    {
        curveColl->add(curve);
    }

    // Create a path.
    Ptr<Path> path = Path::create(curveColl, ChainedCurveOptions::connectedChainedCurves);

    // Create a construction plane at the end of the path.
    Ptr<ConstructionPlaneInput> planeInput = root->constructionPlanes()->createInput();
    planeInput->setByDistanceOnPath(path, ValueInput::createByReal(0));
    Ptr<ConstructionPlane> profPlane = root->constructionPlanes()->add(planeInput);

    // Create a sketch, draw a circle, and get the resulting profile.
    Ptr<Sketch> profSketch = root->sketches()->add(profPlane);
    Ptr<SketchCircle> circle = profSketch->sketchCurves()->sketchCircles()->addByCenterRadius(Point3D::create(0, 0, 0), 1.0);
    Ptr<Profile> prof = profSketch->profiles()->item(0);

    // Create a sweep feature.
    Ptr<SweepFeatures> sweepFeatures = root->features()->sweepFeatures();
    Ptr<SweepFeatureInput> sweepInput = sweepFeatures->createInput(prof, path, FeatureOperations::CutFeatureOperation);
    Ptr<SweepFeature> sweepFeature = sweepFeatures->add(sweepInput);
}
0 Likes
Message 4 of 4

BrianEkins
Mentor
Mentor

It works for me.  Attached is the model I tested with and below is an example of the result and the code.

ProjectSketch.png

 

 

void ProjectTest()
{
	app = Application::get();
	ui = app->userInterface();

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

    // Have the sketch selected and package the curves into an array.
    Ptr<Selection> selection = ui->selectEntity("Select the sketch to Project", "Sketches");
    Ptr<Sketch> skToProj = selection->entity();
    Ptr<SketchCurves> skCurves = skToProj->sketchCurves();
    std::vector<Ptr<Base>> curves(skCurves->count());
    for (int i=0; i < skCurves->count(); ++i)
    {
        curves[i] = skCurves->item(i);
    }

    // Have the face selected and packet it into an array.
    selection = ui->selectEntity("Select the face to project to", "Faces");
    Ptr<BRepFace> faceTarget = selection->entity();
    std::vector<Ptr<BRepFace>> faces(1);
    faces[0] = faceTarget;

    // Create a new sketch to contain the projected curve.
    Ptr<Sketch> newSketch = root->sketches()->add(root->xYConstructionPlane());

    // Project the curves onto the face.
    std::vector<Ptr<SketchEntity>> result;
    result = newSketch->projectToSurface(faces, curves, SurfaceProjectTypes::AlongVectorSurfaceProjectType, root->zConstructionAxis());

    // Package the results into an ObjectCollection.
    Ptr<ObjectCollection> curveColl = ObjectCollection::create();
    for (Ptr<SketchCurve> curve : result)
    {
        curveColl->add(curve);
    }

    // Create a path.
    Ptr<Path> path = Path::create(curveColl, ChainedCurveOptions::connectedChainedCurves);

    // Create a construction plane at the end of the path.
    Ptr<ConstructionPlaneInput> planeInput = root->constructionPlanes()->createInput();
    planeInput->setByDistanceOnPath(path, ValueInput::createByReal(0));
    Ptr<ConstructionPlane> profPlane = root->constructionPlanes()->add(planeInput);

    // Create a sketch, draw a circle, and get the resulting profile.
    Ptr<Sketch> profSketch = root->sketches()->add(profPlane);
    Ptr<SketchCircle> circle = profSketch->sketchCurves()->sketchCircles()->addByCenterRadius(Point3D::create(0, 0, 0), 1.0);
    Ptr<Profile> prof = profSketch->profiles()->item(0);

    // Create a sweep feature.
    Ptr<SweepFeatures> sweepFeatures = root->features()->sweepFeatures();
    Ptr<SweepFeatureInput> sweepInput = sweepFeatures->createInput(prof, path, FeatureOperations::CutFeatureOperation);
    Ptr<SweepFeature> sweepFeature = sweepFeatures->add(sweepInput);
}

 

 

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