-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSketcher_SnapTangent.cxx
438 lines (402 loc) · 11.4 KB
/
Sketcher_SnapTangent.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/**
* \file Sketcher_SnapTangent.cxx
* \brief Implementation file for the class Sketcher_SnapTangent
* \author <a href="mailto:[email protected]?subject=Sketcher_SnapTangent.cxx">Sergei Maslov</a>
*/
#include "Sketcher_SnapTangent.hxx"
IMPLEMENT_STANDARD_RTTIEXT(Sketcher_SnapTangent, Sketcher_Snap)
/**
* \fn Sketcher_SnapTangent()
* \brief Constructs a Sketcher_SnapTangent
*/
Sketcher_SnapTangent::Sketcher_SnapTangent()
{
myTangentType = NothingTangent;
FixFirstPnt = Standard_False;
minAngle = 0;
curAngle = 0;
radiusDistance = 0;
myFirstPnt2d = gp::Origin2d();
tempDir2d = gp::DX2d();
tempGeom2d_Line = new Geom2d_Line(myFirstPnt2d,tempDir2d);
tempGeom2d_Circle = new Geom2d_Circle(gp::OX2d(),0);
myTanRelation = new AIS_TangentRelation(FirstEdge,SecondEdge,myPlane);
}
/**
* \fn ~Sketcher_SnapTangent()
* \brief Destructor
*/
Sketcher_SnapTangent::~Sketcher_SnapTangent()
{
}
/**
* \fn MouseInputEvent(const gp_Pnt2d& thePnt2d)
* \brief input event handler
* \return gp_Pnt2d
* \param thePnt2d const gp_Pnt2d&
*/
gp_Pnt2d Sketcher_SnapTangent::MouseInputEvent(const gp_Pnt2d& tempPnt2d)
{
curPnt2d = tempPnt2d;
switch (myTangentType)
{
case NothingTangent: bestPnt2d = curPnt2d;
break;
case Line_FirstPnt: SelectLine_FirstPntEvent();
if(findbestPnt2d)
{
Geom2dAdaptor_Curve curAdaptor_Curve(tempGeom2d_Curve);
Geom2dGcc_QualifiedCurve curQualifiedCurve (curAdaptor_Curve,GccEnt_unqualified);
Geom2dGcc_Lin2d2Tan curLin2d2Tan(curQualifiedCurve,bestPnt2d,1.0e-10);
if (curLin2d2Tan.NbSolutions() > 0)
{
mylin2d = curLin2d2Tan.ThisSolution(1);
FixFirstPnt = Standard_True;
}
else bestPnt2d = tempPnt2d;
}
break;
case Line_SecondPnt: if (FixFirstPnt)
SelectLine_FixEvent();
else SelectLine_CheckFirstPntEvent();
FixFirstPnt = Standard_False;
break;
case Circle_CenterPnt: SelectCurveCenterEvent();
break;
default: bestPnt2d = curPnt2d;
break;
}
EraseSnap();
return bestPnt2d;
}
/**
* \fn SelectEvent()
* \brief find nearest intersection point
* \return void
*/
void Sketcher_SnapTangent::SelectEvent()
{
switch (myTangentType)
{
case Line_FirstPnt: SelectLine_FirstPntEvent();
break;
case Line_SecondPnt: if (FixFirstPnt)
SelectLine_FixEvent();
else SelectLine_CheckFirstPntEvent();
break;
case Circle_CenterPnt: SelectCurveCenterEvent();
break;
default: bestPnt2d = curPnt2d;
break;
}
}
/**
* \fn GetSnapType()
* \brief get Snap type
* \return Sketcher_SnapType
*/
Sketcher_SnapType Sketcher_SnapTangent::GetSnapType()
{
return SnapTangent;
}
/**
* \fn EraseSnap()
* \brief cancel event handler
* \return void
*/
void Sketcher_SnapTangent::EraseSnap()
{
myContext->Remove(myAIS_Point, true);
myTangentType = NothingTangent;
FixFirstPnt = Standard_False;
}
/**
* \fn DrawRelation()
* \brief draw relation
* \return void
*/
void Sketcher_SnapTangent::DrawRelation()
{
myTanRelation->SetFirstShape(FirstEdge);
myTanRelation->SetSecondShape(SecondEdge);
// if(firstDisplay) myContext->Display(myTanRelation,0,-1);
// else myContext->Redisplay(myTanRelation);
}
/**
* \fn EraseRelation()
* \brief erase relation
* \return void
*/
void Sketcher_SnapTangent::EraseRelation()
{
// myContext->Remove(myTanRelation);
}
/**
* \fn setFirstPnt(const gp_Pnt2d& p)
* \brief set point for exceptions
* \return void
* \param p const gp_Pnt2d&
* \param ttype TangentType
*/
void Sketcher_SnapTangent::setFirstPnt(const gp_Pnt2d& p,TangentType ttype)
{
myFirstPnt2d = p;
myTangentType = ttype;
}
/**
* \fn SelectLine_FirstPntEvent()
* \brief Find nearest point on curve and store it
* \return void
*/
void Sketcher_SnapTangent::SelectLine_FirstPntEvent()
{
findbestPnt2d = Standard_False;
minDistance = minimumSnapDistance;
for( Standard_Integer i=1; i<=data->Length(); i++)
{
mySObject = Handle(Sketcher_Object)::DownCast(data->Value(i));
myGeometryType = mySObject->GetGeometryType();
switch (myGeometryType)
{
case PointSketcherObject: break;
case LineSketcherObject: break;
case CircleSketcherObject: curGeom2d_Circle = Handle(Geom2d_Circle)::DownCast(mySObject->GetGeometry());
ProjectOnCurve.Init(curPnt2d,curGeom2d_Circle);
if(countProject())
{
bestPnt2d = objectPnt2d;
tempGeom2d_Curve = curGeom2d_Circle;
}
break;
case ArcSketcherObject: curGeom2d_Arc = Handle(Geom2d_Arc)::DownCast(mySObject->GetGeometry());
objectPnt2d = curGeom2d_Arc->FirstPnt();
if(count())
{
bestPnt2d = objectPnt2d;
tempGeom2d_Circle->SetCirc2d(curGeom2d_Arc->Circ2d());
tempGeom2d_Curve = tempGeom2d_Circle;
}
objectPnt2d = curGeom2d_Arc->LastPnt();
if(count())
{
bestPnt2d = objectPnt2d;
tempGeom2d_Circle->SetCirc2d(curGeom2d_Arc->Circ2d());
tempGeom2d_Curve = tempGeom2d_Circle;
}
ProjectOnCurve.Init(curPnt2d,curGeom2d_Arc);
if(ProjectOnCurve.NbPoints() > 0)
{
objectPnt2d = ProjectOnCurve.NearestPoint();
curDistance = objectPnt2d.Distance(curPnt2d) + minimumSnapDistance/2;
if(minDistance > curDistance)
{
minDistance = curDistance;
bestPnt2d = objectPnt2d;
tempGeom2d_Circle->SetCirc2d(curGeom2d_Arc->Circ2d());
tempGeom2d_Curve = tempGeom2d_Circle;
}
}
break;
default:break;
}
}
if(minDistance == minimumSnapDistance)
bestPnt2d = curPnt2d;
else findbestPnt2d = Standard_True;
}
/**
* \fn SelectLine_CheckFirstPntEvent()
* \brief check first point of line belonging to circle/arc
* \return void
*/
void Sketcher_SnapTangent::SelectLine_CheckFirstPntEvent()
{
findbestPnt2d = Standard_False;
minDistance = 0.0001;
objectPnt2d = myFirstPnt2d;
for( Standard_Integer i=1; i<=data->Length(); i++)
{
mySObject = Handle(Sketcher_Object)::DownCast(data->Value(i));
myGeometryType = mySObject->GetGeometryType();
switch (myGeometryType)
{
case PointSketcherObject: break;
case LineSketcherObject: break;
case CircleSketcherObject: curGeom2d_Circle = Handle(Geom2d_Circle)::DownCast(mySObject->GetGeometry());
ProjectOnCurve.Init(myFirstPnt2d,curGeom2d_Circle);
if(ProjectOnCurve.NbPoints() > 0)
{
objectPnt2d = ProjectOnCurve.NearestPoint();
curDistance = myFirstPnt2d.Distance(objectPnt2d);
if(minDistance > curDistance)
{
minDistance = curDistance;
bestPnt2d = objectPnt2d;
tempGeom2d_Curve = curGeom2d_Circle;
}
}
break;
case ArcSketcherObject: curGeom2d_Arc = Handle(Geom2d_Arc)::DownCast(mySObject->GetGeometry());
ProjectOnCurve.Init(myFirstPnt2d,curGeom2d_Arc);
if(ProjectOnCurve.NbPoints() > 0)
{
objectPnt2d = ProjectOnCurve.NearestPoint();
curDistance = myFirstPnt2d.Distance(objectPnt2d);
if(minDistance > curDistance)
{
minDistance = curDistance;
bestPnt2d = objectPnt2d;
tempGeom2d_Circle->SetCirc2d(curGeom2d_Arc->Circ2d());
tempGeom2d_Curve = tempGeom2d_Circle;
}
}
break;
default:break;
}
}
if(minDistance != 0.0001)
{
Geom2dAdaptor_Curve curAdaptor_Curve(tempGeom2d_Curve);
Geom2dGcc_QualifiedCurve curQualifiedCurve (curAdaptor_Curve,GccEnt_unqualified);
Geom2dGcc_Lin2d2Tan curLin2d2Tan(curQualifiedCurve,bestPnt2d,1.0e-6);
if (curLin2d2Tan.NbSolutions() > 0)
{
mylin2d = curLin2d2Tan.ThisSolution(1);
FixFirstPnt = Standard_True;
SelectLine_FixEvent();
}
else bestPnt2d = curPnt2d;
}
else SelectLine_SecondPntEvent();
}
/**
* \fn SelectLine_FixEvent()
* \brief find tangent line at first point
* \return void
*/
void Sketcher_SnapTangent::SelectLine_FixEvent()
{
minAngle = MINANGLE*2;
gce_MakeDir2d MD(myFirstPnt2d,curPnt2d);
if(MD.IsDone())
{
tempDir2d = MD.Value();
if(mylin2d.Direction().IsParallel(tempDir2d,minAngle))
{
curAngle = mylin2d.Direction().Angle(tempDir2d);
if (minAngle > curAngle)
minAngle = curAngle;
else minAngle = M_PI - curAngle;
tempGeom2d_Line->SetLin2d(mylin2d);
ProjectOnCurve.Init(curPnt2d,tempGeom2d_Line);
if(ProjectOnCurve.NbPoints() > 0)
bestPnt2d = ProjectOnCurve.NearestPoint();
}
}
if (minAngle == MINANGLE*2)
SelectLine_SecondPntEvent();
else findbestPnt2d = Standard_True;
}
/**
* \fn SelectLine_SecondPntEvent()
* \brief find tangent line at second point
* \return void
*/
void Sketcher_SnapTangent::SelectLine_SecondPntEvent()
{
findbestPnt2d = Standard_False;
minDistance = minimumSnapDistance;
for( Standard_Integer i=1; i<=data->Length(); i++)
{
mySObject = Handle(Sketcher_Object)::DownCast(data->Value(i));
myGeometryType = mySObject->GetGeometryType();
switch (myGeometryType)
{
case PointSketcherObject: break;
case LineSketcherObject: break;
case CircleSketcherObject:
case ArcSketcherObject: curGeom2d_Circle = Handle(Geom2d_Circle)::DownCast(mySObject->GetGeometry());
ProjectOnCurve.Init(curPnt2d,curGeom2d_Circle);
if(countProject())
{
radiusDistance = curGeom2d_Circle->Radius();
tempGeom2d_Curve = curGeom2d_Circle;
}
break;
default:break;
}
}
if(minDistance == minimumSnapDistance)
bestPnt2d = curPnt2d;
else
{
Geom2dAdaptor_Curve curAdaptor_Curve(tempGeom2d_Curve);
Geom2dGcc_QualifiedCurve curQualifiedCurve (curAdaptor_Curve,GccEnt_unqualified);
Geom2dGcc_Lin2d2Tan curLin2d2Tan(curQualifiedCurve,myFirstPnt2d,1.0e-10);
minDistance = radiusDistance;
for(Standard_Integer k=1; k<=curLin2d2Tan.NbSolutions(); k++)
{
tempGeom2d_Line->SetLin2d(curLin2d2Tan.ThisSolution(k));
my2dAPI_InterCurveCurve.Init(tempGeom2d_Line,tempGeom2d_Curve);
if (my2dAPI_InterCurveCurve.NbPoints() > 0)
{
objectPnt2d = my2dAPI_InterCurveCurve.Point(1);
if(count())
bestPnt2d = objectPnt2d;
}
}
if(minDistance == radiusDistance)
bestPnt2d = curPnt2d;
else findbestPnt2d = Standard_True;
}
}
/**
* \fn SelectCurveCenterEvent()
* \brief find tangent circle to curve
* \return void
*/
void Sketcher_SnapTangent::SelectCurveCenterEvent()
{
findbestPnt2d = Standard_False;
minDistance = minimumSnapDistance;
for( Standard_Integer i=1; i<=data->Length(); i++)
{
mySObject = Handle(Sketcher_Object)::DownCast(data->Value(i));
myGeometryType = mySObject->GetGeometryType();
switch (myGeometryType)
{
case PointSketcherObject: break;
case LineSketcherObject:
case CircleSketcherObject:
case ArcSketcherObject:
{
Handle(Geom2d_Curve) curGeom2d_Curve = Handle(Geom2d_Curve)::DownCast(mySObject->GetGeometry());
ProjectOnCurve.Init(curPnt2d,curGeom2d_Curve);
if(countProject())
tempGeom2d_Curve = curGeom2d_Curve;
}
break;
default:break;
}
}
if(minDistance == minimumSnapDistance)
bestPnt2d = curPnt2d;
else
{
ProjectOnCurve.Init(myFirstPnt2d,tempGeom2d_Curve);
if (ProjectOnCurve.NbPoints() > 0)
{
objectPnt2d = ProjectOnCurve.Point(1);
minDistance = curPnt2d.Distance(objectPnt2d);
bestPnt2d = objectPnt2d;
findbestPnt2d = Standard_True;
for( Standard_Integer i=2; i<=ProjectOnCurve.NbPoints(); i++)
{
objectPnt2d = ProjectOnCurve.Point(i);
if(count())
bestPnt2d = objectPnt2d;
}
}
else bestPnt2d = curPnt2d;
}
}