Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

Commit 84bf69a

Browse files
committed
patch
1 parent 65c2e63 commit 84bf69a

8 files changed

Lines changed: 139 additions & 91 deletions

File tree

Lines changed: 79 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
// class SketchConstraintPointOnSegment
1+
// SketchConstraintPointOnSegment.hxx
2+
// Created on: 2025-05-15
3+
// Created by: xAI User
24
//
3-
// Implements a constraint that places a point onto a segment (line or circle)
5+
// Implements a geometric constraint that enforces a point to lie on a segment (line or circle)
6+
// in a 2D sketch. This class ensures a specified point remains on a given line or circle segment
7+
// during constraint solving. It integrates with the sketch constraint solver to manage relationships
8+
// and supports validation and cloning.
49

510
#ifndef SketchConstraintPointOnSegment_HXX
611
#define SketchConstraintPointOnSegment_HXX
@@ -23,114 +28,141 @@
2328
#include "Core/Shapes/Sketch/SketchConstraintSolver.hxx"
2429
#include "Core/Shapes/Sketch/SketchSegment.hxx"
2530

31+
//! \class SketchConstraintPointOnSegment
32+
//! \brief Implements a geometric constraint that enforces a point to lie on a segment (line or circle) in a 2D sketch.
33+
//! This class ensures a specified point remains on a given line or circle segment during constraint solving.
34+
//! It integrates with the sketch constraint solver to manage relationships and supports validation and cloning.
2635
class SketchConstraintPointOnSegment : public SketchConstraint
2736
{
2837
public:
38+
//! \brief Constructor for creating a new point-on-segment constraint.
39+
//! \param[in] point Index of the point to be constrained.
40+
//! \param[in] segment Index of the segment (line or circle) the point should lie on.
41+
//! \param[in] label TDF_Label for storing the constraint in the document (defaults to a new child label).
2942
SketchConstraintPointOnSegment(int point, int segment,
3043
const TDF_Label& label = DocumentTool::SketchConstraintPointOnSegmentsLabel().NewChild())
31-
: SketchConstraint(label.FindChild(0))
32-
, _Label(label)
44+
: SketchConstraint(label.FindChild(0)) //!< Initialize base class with first child label
45+
, _Label(label) //!< Store the provided label
3346
{
34-
assert(!label.HasAttribute());
35-
36-
TDataStd_Name::Set(label, "SketchConstraintPointOnSegment");
37-
38-
SetSegments({segment});
39-
SetPoints({point});
47+
assert(!label.HasAttribute()); //!< Ensure the label is clean
48+
TDataStd_Name::Set(label, "SketchConstraintPointOnSegment"); //!< Set name attribute
49+
SetSegments({segment}); //!< Initialize segment index
50+
SetPoints({point}); //!< Initialize point index
4051
}
4152

53+
//! \brief Constructor for loading an existing constraint from a TDF_Label.
54+
//! \param[in] label TDF_Label containing the constraint data.
4255
SketchConstraintPointOnSegment(const TDF_Label& label)
43-
: SketchConstraint(label.FindChild(0))
44-
, _Label(label)
56+
: SketchConstraint(label.FindChild(0)) //!< Initialize base class with first child label
57+
, _Label(label) //!< Store the provided label
4558
{
46-
assert(label.HasAttribute());
59+
assert(label.HasAttribute()); //!< Ensure the label has attributes
4760
}
4861

62+
//! \brief Return the TDF_Label associated with this constraint.
63+
//! \return The TDF_Label storing the constraint data.
4964
TDF_Label Label() const { return _Label; }
5065

66+
//! \brief Apply the constraint to the solver.
67+
//! \param[in] points Map of point indices to their 2D coordinates.
68+
//! \param[in] segments Map of segment indices to their SketchSegment handles.
69+
//! \param[in] solver Handle to the constraint solver.
70+
//! \return True if the constraint was successfully applied, false otherwise.
5171
virtual bool MakeConstraint(const std::map<int, gp_Pnt2d>& points,
5272
const std::map<int, Handle(SketchSegment)>& segments,
5373
const Handle(SketchConstraintSolver)& solver) override
5474
{
75+
// Check if the segment exists using std::map::find
5576
auto it = segments.find(Segment(0));
5677
if(it == segments.end())
5778
{
58-
return false;
79+
return false; //!< Segment not found
5980
}
6081

6182
bool valid = false;
62-
Handle(SketchSegment) segment = it->second;
83+
Handle(SketchSegment) segment = it->second; //!< Get segment handle
6384

85+
// Handle line segment
6486
if(Handle(SketchSegmentLine) line = Handle(SketchSegmentLine)::DownCast(segment))
6587
{
6688
Constraint con;
67-
con.type = ConstraintType::PointOnLine;
68-
89+
con.type = ConstraintType::PointOnLine; //!< Set constraint type
6990
valid = solver->SetLine(con.line1, line->PointsAttribute()->Value(0),
70-
line->PointsAttribute()->Value(1), true, true);
71-
valid &= solver->SetPoint(con.point1, Point(0), false);
72-
91+
line->PointsAttribute()->Value(1), true, true); //!< Configure line
92+
valid &= solver->SetPoint(con.point1, Point(0), false); //!< Set point
7393
if(valid)
7494
{
75-
solver->AddConstraint(con);
95+
solver->AddConstraint(con); //!< Add constraint to solver
7696
}
7797
}
98+
// Handle circle segment
7899
else if(Handle(SketchSegmentCircle) circle = Handle(SketchSegmentCircle)::DownCast(segment))
79100
{
80101
std::vector<Constraint> constraints;
81-
82102
Constraint con;
83-
con.type = ConstraintType::PointOnCircle;
84-
85-
valid = solver->SetCircle(con.circle1, circle, constraints, points, true, false);
86-
valid &= solver->SetPoint(con.point1, Point(0), false);
87-
103+
con.type = ConstraintType::PointOnCircle; //!< Set constraint type
104+
valid = solver->SetCircle(con.circle1, circle, constraints, points, true, false); //!< Configure circle
105+
valid &= solver->SetPoint(con.point1, Point(0), false); //!< Set point
88106
constraints.push_back(con);
89-
90107
if(valid)
91108
{
92109
for(const auto& c : constraints)
93110
{
94-
solver->AddConstraint(c);
111+
solver->AddConstraint(c); //!< Add each constraint to solver
95112
}
96113
}
97114
}
98115

99-
return valid;
116+
return valid; //!< Return success status
100117
}
101118

119+
//! \brief Create a clone of this constraint.
120+
//! \return A new instance of SketchConstraintPointOnSegment with the same point and segment indices.
102121
virtual Handle(SketchConstraint) Clone() const override
103122
{
104123
return new SketchConstraintPointOnSegment(Point(0), Segment(0));
105124
}
106125

126+
//! \brief Check if a point-on-segment constraint can be created.
127+
//! \param[in] sketch Handle to the sketch containing points and segments.
128+
//! \param[in] points Vector of point indices.
129+
//! \param[in] segments Vector of segment indices.
130+
//! \return True if the constraint is valid and can be created, false otherwise.
107131
static bool CanCreate(const Handle(Sketch)& sketch,
108132
const std::vector<int>& points,
109133
const std::vector<int>& segments)
110134
{
111-
return points.size() > 0
112-
&& segments.size() == 1
113-
&& (SketchConstraintHelper::AllSegmentsOfType<SketchSegmentLine>(sketch, segments)
114-
|| SketchConstraintHelper::AllSegmentsOfType<SketchSegmentCircle>(sketch, segments))
115-
&& !SketchConstraintHelper::AnySegmentAndPointConstraint<SketchConstraintPointOnSegment>(sketch, points, segments)
116-
&& !SketchConstraintHelper::AnySegmentAndPointConstraint<SketchConstraintPointOnMidpoint>(sketch, points, segments)
117-
&& !SketchConstraintHelper::AnyConstrainedPoint<SketchConstraintFixed>(sketch, points);
135+
return points.size() > 0 //!< At least one point
136+
&& segments.size() == 1 //!< Exactly one segment
137+
&& (SketchConstraintHelper::AllSegmentsOfType<SketchSegmentLine>(sketch, segments) ||
138+
SketchConstraintHelper::AllSegmentsOfType<SketchSegmentCircle>(sketch, segments)) //!< Line or circle
139+
&& !SketchConstraintHelper::AnySegmentAndPointConstraint<SketchConstraintPointOnSegment>(
140+
sketch, points, segments) //!< No existing point-on-segment
141+
&& !SketchConstraintHelper::AnySegmentAndPointConstraint<SketchConstraintPointOnMidpoint>(
142+
sketch, points, segments) //!< No midpoint constraint
143+
&& !SketchConstraintHelper::AnyConstrainedPoint<SketchConstraintFixed>(
144+
sketch, points); //!< No fixed point
118145
}
119146

147+
//! \brief Create point-on-segment constraints for the given points and segment.
148+
//! \param[in] sketch Handle to the sketch containing points and segments.
149+
//! \param[in] points Vector of point indices.
150+
//! \param[in] segments Vector of segment indices (must contain exactly one segment).
151+
//! \return A vector of created constraint handles.
120152
static std::vector<Handle(SketchConstraint)> Create(const Handle(Sketch)& sketch,
121153
const std::vector<int>& points,
122154
const std::vector<int>& segments)
123155
{
124156
std::vector<Handle(SketchConstraint)> list;
125157

126-
auto it = sketch->Segments().find(segments[0]);
127-
if(it == sketch->Segments().end())
128-
{
129-
return list;
130-
}
158+
// Find segment in sketch
159+
auto sketchSegments = sketch->Segments();
160+
auto it = sketchSegments.find(segments[0]);
161+
if(it == sketchSegments.end()) return list; //!< Segment not found
131162

132-
Handle(SketchSegment) segment = it->second;
163+
Handle(SketchSegment) segment = it->second; //!< Get segment handle
133164

165+
// Create constraints for valid points
134166
for(const auto& pointIndex : points)
135167
{
136168
bool contains = false;
@@ -144,17 +176,17 @@ public:
144176
}
145177
if(!contains)
146178
{
147-
list.push_back(new SketchConstraintPointOnSegment(pointIndex, segments[0]));
179+
list.push_back(new SketchConstraintPointOnSegment(pointIndex, segments[0])); //!< Create constraint
148180
}
149181
}
150182

151-
return list;
183+
return list; //!< Return created constraints
152184
}
153185

154186
private:
155-
TDF_Label _Label;
187+
TDF_Label _Label; //!< TDF_Label for storing the constraint in the document
156188
};
157189

158190
DEFINE_STANDARD_HANDLE(SketchConstraintPointOnSegment, SketchConstraint)
159191

160-
#endif // SketchConstraintPointOnSegment_HXX
192+
#endif // SketchConstraintPointOnSegment_HXX

src/Iact/Workspace/Snap3D.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ SnapInfo Snap3D::Snap(const Handle(MouseEventData)& mouseEvent)
2020

2121
snapInfo = Snap(screenPoint, shape, aisObject);
2222

23-
if(snapInfo.mode != SnapModes::None)
23+
if(snapInfo.mode != SnapMode::None)
2424
{
2525
snapInfo.targetName = mouseEvent->GetDetectedEntity()->get_type_name();
2626
}

src/Iact/Workspace/Snap3D.hxx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ class Snap3D : public SnapBase
1515
public:
1616
Snap3D()
1717
{
18-
SetSupportedModes(static_cast<SnapModes>(
19-
static_cast<int>(SnapModes::Grid) |
20-
static_cast<int>(SnapModes::Vertex) |
21-
static_cast<int>(SnapModes::Edge)));
18+
SetSupportedModes(SnapMode::Grid | SnapMode::Vertex | SnapMode::Edge);
2219
}
2320

2421
DEFINE_STANDARD_RTTIEXT(Snap3D, SnapBase)
@@ -28,7 +25,7 @@ public:
2825
SnapInfo snapInfo;
2926

3027
auto [mode, point] = SnapToShape(screenPoint, shape);
31-
if(mode != SnapModes::None)
28+
if(mode != SnapMode::None)
3229
{
3330
snapInfo.mode = mode;
3431
snapInfo.point = point;

src/Iact/Workspace/SnapBase.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "Iact/Workspace/SnapBase.hxx"
22

3-
const SnapInfo SnapInfo::Empty = SnapInfo(SnapModes::None, gp_Pnt());
3+
const SnapInfo SnapInfo::Empty = SnapInfo(SnapMode::None, gp_Pnt());
44

55
IMPLEMENT_STANDARD_RTTIEXT(SnapBase, Standard_Transient)

src/Iact/Workspace/SnapBase.hxx

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,10 @@
2323
#include <TopoDS_Shape.hxx>
2424
#include <TopoDS_Vertex.hxx>
2525

26+
#include "Iact/Workspace/SnapMode.hxx"
27+
2628
class WorkspaceController;
2729

28-
// 捕捉模式枚举
29-
enum class SnapModes
30-
{
31-
None = 0,
32-
Grid = 1 << 0,
33-
Vertex = 1 << 1,
34-
Edge = 1 << 2,
35-
Face = 1 << 3
36-
};
3730

3831
// 捕捉信息结构体
3932
struct SnapInfo
@@ -42,7 +35,7 @@ struct SnapInfo
4235
gp_Pnt point;
4336
std::string targetName;
4437

45-
SnapInfo(SnapModes m = SnapModes::None, const gp_Pnt& p = gp_Pnt())
38+
SnapInfo(SnapModes m = SnapMode::None, const gp_Pnt& p = gp_Pnt())
4639
: mode(m), point(p), targetName("")
4740
{}
4841

@@ -64,7 +57,7 @@ public:
6457
class SnapBase : public Standard_Transient, public ISnapHandler
6558
{
6659
public:
67-
SnapBase() : supportedModes_(SnapModes::None), currentInfo_(SnapInfo::Empty) {}
60+
SnapBase() : supportedModes_(SnapMode::None), currentInfo_(SnapInfo::Empty) {}
6861
virtual ~SnapBase() = default;
6962

7063
DEFINE_STANDARD_RTTIEXT(SnapBase, Standard_Transient)
@@ -82,16 +75,16 @@ protected:
8275
{
8376
if(shape.IsNull())
8477
{
85-
return {SnapModes::None, gp_Pnt()};
78+
return {SnapMode::None, gp_Pnt()};
8679
}
8780

88-
if(HasFlag(supportedModes_, SnapModes::Vertex) && shape.ShapeType() == TopAbs_VERTEX)
81+
if((supportedModes_ & SnapMode::Vertex) && shape.ShapeType() == TopAbs_VERTEX)
8982
{
9083
TopoDS_Vertex vertex = TopoDS::Vertex(shape);
91-
return {SnapModes::Vertex, BRep_Tool::Pnt(vertex)};
84+
return {SnapMode::Vertex, BRep_Tool::Pnt(vertex)};
9285
}
9386

94-
if(HasFlag(supportedModes_, SnapModes::Edge) && shape.ShapeType() == TopAbs_EDGE)
87+
if((supportedModes_ & SnapMode::Edge) && shape.ShapeType() == TopAbs_EDGE)
9588
{
9689
TopoDS_Edge edge = TopoDS::Edge(shape);
9790
Standard_Real umin, umax;
@@ -110,37 +103,31 @@ protected:
110103
{
111104
gp_Pnt p1, p2;
112105
extrema.NearestPoints(p1, p2);
113-
return {SnapModes::Edge, p1};
106+
return {SnapMode::Edge, p1};
114107
}
115108
}
116109
}
117110

118-
return {SnapModes::None, gp_Pnt()};
111+
return {SnapMode::None, gp_Pnt()};
119112
}
120113

121114
std::pair<SnapModes, gp_Pnt> SnapToAisObject(const Graphic3d_Vec2i& Graphic3d_Vec2i, const Handle(AIS_InteractiveObject)& aisObject)
122115
{
123116
if(aisObject.IsNull())
124117
{
125-
return {SnapModes::None, gp_Pnt()};
118+
return {SnapMode::None, gp_Pnt()};
126119
}
127120

128-
if(HasFlag(supportedModes_, SnapModes::Vertex))
121+
if((supportedModes_ & SnapMode::Vertex))
129122
{
130123
Handle(AIS_Point) aisPoint = Handle(AIS_Point)::DownCast(aisObject);
131124
if(!aisPoint.IsNull())
132125
{
133-
return {SnapModes::Vertex, aisPoint->Component()->Pnt()};
126+
return {SnapMode::Vertex, aisPoint->Component()->Pnt()};
134127
}
135128
}
136129

137-
return {SnapModes::None, gp_Pnt()};
138-
}
139-
140-
// 辅助函数:检查标志位
141-
bool HasFlag(SnapModes modes, SnapModes flag) const
142-
{
143-
return (static_cast<int>(modes) & static_cast<int>(flag)) != 0;
130+
return {SnapMode::None, gp_Pnt()};
144131
}
145132

146133
protected:

src/Iact/Workspace/SnapMode.hxx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
#ifndef SnapMode_HXX
3+
#define SnapMode_HXX
4+
5+
// ²¶×½Ä£Ê½Ã¶¾Ù
6+
enum class SnapMode
7+
{
8+
None,
9+
Grid,
10+
Vertex,
11+
Edge,
12+
Face
13+
};
14+
15+
DEFINE_ENUM_FLAGS(SnapModes, SnapMode)
16+
17+
#endif // ! SnapMode_HXX
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "Iact/Workspace/SnapOnCurve2D.hxx"
2+
3+
const std::shared_ptr<SnapInfoCurve2D> SnapInfoCurve2D::Empty =
4+
std::make_shared<SnapInfoCurve2D>(SnapMode::None, gp_Pnt2d(0, 0), 0);
5+

0 commit comments

Comments
 (0)