
Sun, 05/17/2020 - 09:01
Forums:
I don't know if you have done such an effect? (Reference picture projectq & a.png)
1 is a closed curve
5 is a curved surface
Now I want to project 1 onto 5 and divide 5 into two parts.
I used the following code to achieve the effect in the figure.
2 is the curve of 1 projected onto the surface of 5, and 4 is the new surface generated from 2 and 5, the original surface 5 is divided into new surface 4 and surface 5. The problem now is that there is more curve 3, which is wrong. Do not you know why? Thank you all.
code show as below:
void CProjectToFace::build() {
if (myAISContext->NbSelected() != 2) {
return;
}
myAISContext->InitSelected();
TopoDS_Shape shape1 = myAISContext->SelectedShape();
if (shape1.IsNull()) {
return;
}
myAISContext->NextSelected();
TopoDS_Shape shape2 = myAISContext->SelectedShape();
if (shape2.IsNull()) {
return;
}
TopAbs_ShapeEnum shapetype = shape1.ShapeType();
if ((shapetype != TopAbs_FACE) && (shapetype != TopAbs_EDGE) && (shapetype != TopAbs_WIRE)) {
return;
}
TopoDS_Shape wireshape;
if (shapetype == TopAbs_FACE) {
BRepBuilderAPI_MakeWire makewire;
TopExp_Explorer explorer;
explorer.Init(shape1, TopAbs_EDGE);
for (; explorer.More(); explorer.Next()) {
makewire.Add(TopoDS::Edge(explorer.Value()));
}
wireshape = makewire.Shape();
if (wireshape.IsNull()) {
return;
}
} else {
wireshape = shape1;
}
// Calculate the projection surface closest to the projection curve
TopoDS_Face projectface;
GProp_GProps System;
BRepGProp::LinearProperties(wireshape, System);
gp_Pnt G = System.CentreOfMass();
Standard_Real mindistance = -1;
TopExp_Explorer explorer;
explorer.Init(shape2, TopAbs_FACE);
for (; explorer.More(); explorer.Next()) {
TopoDS_Face face = TopoDS::Face(explorer.Value());
BRepAdaptor_Surface aSurface(face);
Standard_Real u1, u2, v1, v2;
u1 = aSurface.FirstUParameter();
u2 = aSurface.LastUParameter();
v1 = aSurface.FirstVParameter();
v2 = aSurface.LastVParameter();
gp_Pnt aCenterOfFace;
gp_Vec aVec1, aVec2, aNormalOfFace;
aSurface.D1((u1 + u2) / 2, (v1 + v2) / 2, aCenterOfFace, aVec1, aVec2);
Standard_Real distance = aCenterOfFace.SquareDistance(G);
if (mindistance < 0) {
mindistance = distance;
projectface = face;
} else {
if (mindistance > distance) {
mindistance = distance;
projectface = face;
}
}
}
if (projectface.IsNull()) {
return;
}
// Project the projection curve onto the projection surface
BRepAlgo_NormalProjection projection(projectface);
projection.Add(wireshape);
projection.Build();
if (projection.IsDone()) {
// Convert projection results to wireframe
TopoDS_Shape shape = projection.Projection();
BRepBuilderAPI_MakeWire wire;
TopExp_Explorer exploreredge(shape, TopAbs_EDGE);
for (; exploreredge.More(); exploreredge.Next()) {
TopoDS_Edge edge = TopoDS::Edge(exploreredge.Value());
wire.Add(edge);
}
wire.Build();
if (wire.IsDone()) {
if (!wire.Shape().IsNull()) {
// Add the projected wireframe to the projection surface and split the projection surface
BRepFeat_SplitShape splitShape(shape2);
splitShape.Add(wire.Wire(), projectface);
splitShape.Build();
if (splitShape.IsDone()) {
TopoDS_Shape aNewShape = splitShape.Shape();
if (!aNewShape.IsNull()) {
// Stitch the new projection surface into a solid
BRepBuilderAPI_Sewing aSewingTool;
aSewingTool.Load(aNewShape);
aSewingTool.Perform();
TopoDS_Shape sewedshape = aSewingTool.SewedShape();
bool issolid = false;
if (sewedshape.ShapeType() == TopAbs_SHELL) {
BRepBuilderAPI_MakeSolid makeSolid(TopoDS::Shell(sewedshape));
makeSolid.Build();
if (makeSolid.IsDone()) {
issolid = true;
// Redisplay projection results
Handle(CADModelShape)::DownCast(myAISContext->SelectedInteractive())->Set(
makeSolid.Shape());
myAISContext->Redisplay(myAISContext->SelectedInteractive(), true);
}
}
if (!issolid) {
// Redisplay projection results
Handle(CADModelShape)::DownCast(myAISContext->SelectedInteractive())->Set(
sewedshape);
myAISContext->Redisplay(myAISContext->SelectedInteractive(), true);
}
}
}
}
}
}
}
Attachments: