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
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.
2635class SketchConstraintPointOnSegment : public SketchConstraint
2736{
2837public:
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
154186private:
155- TDF_Label _Label;
187+ TDF_Label _Label; // !< TDF_Label for storing the constraint in the document
156188};
157189
158190DEFINE_STANDARD_HANDLE (SketchConstraintPointOnSegment, SketchConstraint)
159191
160- #endif // SketchConstraintPointOnSegment_HXX
192+ #endif // SketchConstraintPointOnSegment_HXX
0 commit comments