zalo / CascadeStudio

A Full Live-Scripted CAD Kernel in the Browser
https://zalo.github.io/CascadeStudio/
MIT License
994 stars 122 forks source link

Trying to call BRepOffsetAPI_MakeThickSolid #33

Closed gulamf closed 3 years ago

gulamf commented 3 years ago

Firstly thank you for making such a good tool.

I'm trying to go through the Open CASCADE tutorial using the oc namespace. https://old.opencascade.com/doc/occt-7.0.0/overview/html/occt__tutorial.html

Im stuck calling BRepOffsetAPI_MakeThickSolid and am getting an error in the console. "Line 9943664: RuntimeError: indirect call to null"

there is my code for now.

var myHeight = 70;
var myWidth  = 50;
var myThickness  = 30;

// define Geomertic points
var aPnt1  = new oc.gp_Pnt(-myWidth / 2., 0, 0);
var aPnt2 = new oc.gp_Pnt(-myWidth / 2., -myThickness / 4., 0);
var aPnt3 = new oc.gp_Pnt (0, -myThickness / 2., 0);
var aPnt4 = new oc.gp_Pnt (myWidth / 2., -myThickness / 4., 0);
var aPnt5 = new oc.gp_Pnt (myWidth / 2., 0, 0);

// define Geometric Arcs and lines
var aArcOfCircle = new oc.GC_MakeArcOfCircle(aPnt2,aPnt3,aPnt4).Value();
var aSegment1    = new oc.GC_MakeSegment(aPnt1, aPnt2).Value();
var aSegment2    = new oc.GC_MakeSegment(aPnt4, aPnt5).Value();

// define edges
var  aEdge1 = new oc.BRepBuilderAPI_MakeEdge(aSegment1).Edge();
var  aEdge2 = new oc.BRepBuilderAPI_MakeEdge(aArcOfCircle).Edge();
var  aEdge3 = new oc.BRepBuilderAPI_MakeEdge(aSegment2).Edge();

// define wire
var  aWire = new oc.BRepBuilderAPI_MakeWire(aEdge1, aEdge2, aEdge3).Wire();

// define x axis to mirror the wire
var xAxis = oc.gp.prototype.OX();

// define transformation
var aTrsf = new oc.gp_Trsf() ;
aTrsf.SetMirror(xAxis);

// perform the transform
var aBRepTrsf = new oc.BRepBuilderAPI_Transform(aWire, aTrsf);

var aMirroredShape = aBRepTrsf.Shape();

var  aMirroredWire = oc.TopoDS.prototype.Wire(aMirroredShape);

var mkWire = new oc.BRepBuilderAPI_MakeWire() ;
mkWire.Add(aWire);
mkWire.Add(aMirroredWire);

var myWireProfile = new oc.TopoDS_Wire( mkWire.Wire());

 var myFaceProfile = new oc.BRepBuilderAPI_MakeFace(myWireProfile).Face();

var aPrismVec = new oc.gp_Vec(0, 0, myHeight);

var myBody = new oc.BRepPrimAPI_MakePrism(myFaceProfile, aPrismVec).Shape();

var mkFillet = new oc.BRepFilletAPI_MakeFillet(myBody);

var anEdgeExplorer = new oc.TopExp_Explorer(myBody, oc.TopAbs_EDGE);

while(anEdgeExplorer.More()){
    var anEdge = oc.TopoDS.prototype.Edge(anEdgeExplorer.Current());
        mkFillet.Add(myThickness / 12., anEdge);
    anEdgeExplorer.Next();
}

myBody = mkFillet.Shape();

var neckLocation = new oc.gp_Pnt (0, 0, myHeight);
var neckAxis = oc.gp.prototype.DZ();
var neckAx2 = new oc.gp_Ax2(neckLocation, neckAxis);

var  myNeckRadius = myThickness / 4.;
var myNeckHeight = myHeight / 10;
var MKCylinder = new oc.BRepPrimAPI_MakeCylinder (neckAx2, myNeckRadius, myNeckHeight);
var myNeck = MKCylinder.Shape();

myBody = new oc.BRepAlgoAPI_Fuse(myBody, myNeck).Shape();

var faceToRemove;
var zMax = -1;

for(var aFaceExplorer = new oc.TopExp_Explorer(myBody, oc.TopAbs_FACE) ; aFaceExplorer.More() ; aFaceExplorer.Next()){
    var aFace = oc.TopoDS.prototype.Face(aFaceExplorer.Current());

    var aSurface = oc.BRep_Tool.prototype.Surface(aFace).get();

    if(aSurface.DynamicType().get().Name() == 'Geom_Plane'){
        var aPlane = new oc.Handle_Geom_Plane(aSurface.DynamicType().get()).get();
        var aPnt = aPlane.Location();
        var  aZ = aPnt.Z();
        if(aZ > zMax){
            zMax = aZ;
            faceToRemove = aFace;
        }
    }
}
var facesToRemove = new oc.TopTools_ListOfShape();
facesToRemove.Append(faceToRemove);
var MyBody = new oc.BRepOffsetAPI_MakeThickSolid().MakeThickSolidByJoin(myBody, facesToRemove, -myThickness / 50,1e-3);

sceneShapes.push(MyBody);
gulamf commented 3 years ago

Solved. The I was looking at the old tutorial document. The new one is here. https://dev.opencascade.org/doc/overview/html/occt__tutorial.html

The constructor was depreciated and usage is totally different.

updated code for reference.

for(var aFaceExplorer = new oc.TopExp_Explorer(myBody, oc.TopAbs_FACE) ; aFaceExplorer.More() ; aFaceExplorer.Next()){
    var aFace = oc.TopoDS.prototype.Face(aFaceExplorer.Current());

    var aSurface = oc.BRep_Tool.prototype.Surface(aFace);

    if(aSurface.get().DynamicType().get().Name() == oc.Geom_Plane.prototype.get_type_name()){

        var aPlane = new oc.Handle_Geom_Plane(aSurface.get()).get();

        var aPnt = aPlane.Location();
        var  aZ = aPnt.Z();
        if(aZ > zMax){
            zMax = aZ;
            faceToRemove = aFace;
        }
    }
}
var facesToRemove = new oc.TopTools_ListOfShape();
facesToRemove.Append(faceToRemove);
var bodyMaker = new oc.BRepOffsetAPI_MakeThickSolid();
bodyMaker.MakeThickSolidByJoin(myBody, faceToRemove, -myThickness / 50,0.1);

sceneShapes.push(bodyMaker.Shape());
gulamf commented 3 years ago

Just for interest. Here is the finished working example.

var myHeight = 70;
var myWidth  = 50;
var myThickness  = 30;

// define Geomertic points
var aPnt1  = new oc.gp_Pnt(-myWidth / 2., 0, 0);
var aPnt2 = new oc.gp_Pnt(-myWidth / 2., -myThickness / 4., 0);
var aPnt3 = new oc.gp_Pnt (0, -myThickness / 2., 0);
var aPnt4 = new oc.gp_Pnt (myWidth / 2., -myThickness / 4., 0);
var aPnt5 = new oc.gp_Pnt (myWidth / 2., 0, 0);

// define Geometric Arcs and lines
var aArcOfCircle = new oc.GC_MakeArcOfCircle(aPnt2,aPnt3,aPnt4).Value();
var aSegment1    = new oc.GC_MakeSegment(aPnt1, aPnt2).Value();
var aSegment2    = new oc.GC_MakeSegment(aPnt4, aPnt5).Value();

// define edges
var  aEdge1 = new oc.BRepBuilderAPI_MakeEdge(aSegment1).Edge();
var  aEdge2 = new oc.BRepBuilderAPI_MakeEdge(aArcOfCircle).Edge();
var  aEdge3 = new oc.BRepBuilderAPI_MakeEdge(aSegment2).Edge();

// define wire
var  aWire = new oc.BRepBuilderAPI_MakeWire(aEdge1, aEdge2, aEdge3).Wire();

// define x axis to mirror the wire

var xAxis = oc.gp.prototype.OX();
// define transformation
var aTrsf = new oc.gp_Trsf() ;
aTrsf.SetMirror(xAxis);

// perform the transform
var aBRepTrsf = new oc.BRepBuilderAPI_Transform(aWire, aTrsf);

var aMirroredShape = aBRepTrsf.Shape();

var  aMirroredWire = oc.TopoDS.prototype.Wire(aMirroredShape);

var mkWire = new oc.BRepBuilderAPI_MakeWire() ;
mkWire.Add(aWire);
mkWire.Add(aMirroredWire);
var myWireProfile = new oc.TopoDS_Wire( mkWire.Wire());

 var myFaceProfile = new oc.BRepBuilderAPI_MakeFace(myWireProfile).Face();

var aPrismVec = new oc.gp_Vec(0, 0, myHeight);

var liquidBody = new oc.TopoDS_Shape(new oc.BRepPrimAPI_MakePrism(myFaceProfile, aPrismVec).Shape());

var mkFillet = new oc.BRepFilletAPI_MakeFillet(liquidBody);

var anEdgeExplorer = new oc.TopExp_Explorer(liquidBody, oc.TopAbs_EDGE);

while(anEdgeExplorer.More()){
    var anEdge = oc.TopoDS.prototype.Edge(anEdgeExplorer.Current());
        mkFillet.Add(myThickness / 12., anEdge);
    anEdgeExplorer.Next();
}

var filletedBody = mkFillet;

var neckLocation = new oc.gp_Pnt (0, 0, myHeight);
var neckAxis = oc.gp.prototype.DZ();
var neckAx2 = new oc.gp_Ax2(neckLocation, neckAxis);

var  myNeckRadius = myThickness / 4.;
var myNeckHeight = myHeight / 10;
var MKCylinder = new oc.BRepPrimAPI_MakeCylinder (neckAx2, myNeckRadius, myNeckHeight);
var myNeck = MKCylinder;

var solidBottle = new oc.BRepAlgoAPI_Fuse(filletedBody.Shape(), myNeck.Shape());

var faceToRemove;
var zMax = -1;

for(var aFaceExplorer = new oc.TopExp_Explorer(solidBottle.Shape(), oc.TopAbs_FACE) ; aFaceExplorer.More() ; aFaceExplorer.Next()){
    var aFace = new oc.TopoDS_Face(new oc.TopoDS.prototype.Face(aFaceExplorer.Current()));

    var aSurface = oc.BRep_Tool.prototype.Surface(aFace);

    if(aSurface.get().DynamicType().get().Name() == oc.Geom_Plane.prototype.get_type_name()){

        var aPlane = new oc.Handle_Geom_Plane(aSurface.get()).get();

        var aPnt = aPlane.Location();
        var  aZ = aPnt.Z();
        if(aZ > zMax){
            zMax = aZ;
            faceToRemove = aFace;
        }

    }
}

var facesToRemove = new oc.TopTools_ListOfShape();
facesToRemove.Append(faceToRemove);
var bodyMaker = new oc.BRepOffsetAPI_MakeThickSolid();
bodyMaker.MakeThickSolidByJoin(solidBottle.Shape(), facesToRemove, -myThickness / 50,1.e-3);
var hollowBottle = bodyMaker.Shape();

var aCyl1 = new oc.Handle_Geom_Surface(new oc.Geom_CylindricalSurface(neckAx2, myNeckRadius * 0.99));
var aCyl2 = new oc.Handle_Geom_Surface(new oc.Geom_CylindricalSurface(neckAx2, myNeckRadius * 1.05));

oc.M_PI = 3.14159265359;

var aPnt = new oc.gp_Pnt2d(2. * oc.M_PI, myNeckHeight / 2.);
var aDir = new oc.gp_Dir2d(2. * oc.M_PI, myNeckHeight / 4.);
var anAx2d = new oc.gp_Ax2d(aPnt, aDir);

var aMajor = 2. * oc.M_PI;
var aMinor = myNeckHeight / 10;
var anEllipse1 = new oc.Handle_Geom2d_Curve(new oc.Geom2d_Ellipse(anAx2d, aMajor, aMinor));
var anEllipse2 = new oc.Handle_Geom2d_Curve(new oc.Geom2d_Ellipse(anAx2d, aMajor, aMinor / 4));

var anArc1 = new oc.Handle_Geom2d_TrimmedCurve(new oc.Geom2d_TrimmedCurve(anEllipse1, 0, oc.M_PI));
var anArc2 = new oc.Handle_Geom2d_TrimmedCurve(new oc.Geom2d_TrimmedCurve(anEllipse2, 0, oc.M_PI));

var anEllipsePnt1 = new oc.gp_Pnt2d(anEllipse1.get().Value(0).X(),anEllipse1.get().Value(0).Y());
var anEllipsePnt2 = new oc.gp_Pnt2d(anEllipse1.get().Value(oc.M_PI).X(),anEllipse1.get().Value(oc.M_PI).Y());

var aSegment = new oc.GCE2d_MakeSegment(anEllipsePnt1, anEllipsePnt2).Value();

var anEdge1OnSurf1 = new oc.BRepBuilderAPI_MakeEdge(anArc1,  aCyl1).Edge();
var anEdge2OnSurf1 = new oc.BRepBuilderAPI_MakeEdge(aSegment, aCyl1).Edge();
var anEdge1OnSurf2 = new oc.BRepBuilderAPI_MakeEdge(anArc2, aCyl2).Edge();
var anEdge2OnSurf2 = new oc.BRepBuilderAPI_MakeEdge(aSegment, aCyl2).Edge();

var threadingWire1 = new oc.BRepBuilderAPI_MakeWire(anEdge1OnSurf1, anEdge2OnSurf1).Wire();
var threadingWire2 = new oc.BRepBuilderAPI_MakeWire(anEdge1OnSurf2, anEdge2OnSurf2).Wire();

oc.BRepLib.prototype.BuildCurves3d(threadingWire1);
oc.BRepLib.prototype.BuildCurves3d(threadingWire2);

var aTool = new oc.BRepOffsetAPI_ThruSections (true);
aTool.AddWire(threadingWire1);
aTool.AddWire(threadingWire2);
aTool.CheckCompatibility(false);
var myThreading = aTool.Shape();

var aRes = new oc.TopoDS_Compound() ;
var aBuilder = new oc.BRep_Builder() ;
aBuilder.MakeCompound (aRes);
aBuilder.Add (aRes, hollowBottle);
aBuilder.Add (aRes, myThreading);

sceneShapes.push(aRes);