forked from solvespace/solvespace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVbDemo.vb
1252 lines (1088 loc) · 48.6 KB
/
VbDemo.vb
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'-----------------------------------------------------------------------------
' Some sample code for slvs.dll. We draw some geometric entities, provide
' initial guesses for their positions, and then constrain them. The solver
' calculates their new positions, in order to satisfy the constraints.
'
' The library is distributed as a DLL, but the functions are designed to
' be usable from .net languages through a P/Invoke. This file contains an
' example of that process, and a wrapper class around those P/Invoke'd
' functions that you may wish to use a starting point in your own
' application.
'
' Copyright 2008-2013 Jonathan Westhues.
'-----------------------------------------------------------------------------
Imports System.Runtime.InteropServices
Module VbDemo
' Call our example functions, which set up some kind of sketch, solve
' it, and then print the result.
Sub Main()
Console.WriteLine("EXAMPLE IN 3d (by objects):")
Example3dWithObjects()
Console.WriteLine("")
Console.WriteLine("EXAMPLE IN 2d (by objects):")
Example2dWithObjects()
Console.WriteLine("")
Console.WriteLine("EXAMPLE IN 3d (by handles):")
Example3dWithHandles()
Console.WriteLine("")
Console.WriteLine("EXAMPLE IN 2d (by handles):")
Example2dWithHandles()
Console.WriteLine("")
End Sub
'''''''''''''''''''''''''''''''
' This is the simplest way to use the library. A set of wrapper
' classes allow us to represent entities (e.g., lines and points)
' as .net objects. So we create an Slvs object, which will contain
' the entire sketch, with all the entities and constraints.
'
' We then create entity objects (for example, points and lines)
' associated with that sketch, indicating the initial positions of
' those entities and any hierarchical relationships among them (e.g.,
' defining a line entity in terms of endpoint entities). We also add
' constraints associated with those entities.
'
' Finally, we solve, and print the new positions of the entities. If the
' solution succeeded, then the entities will satisfy the constraints. If
' not, then the solver will suggest problematic constraints that, if
' removed, would render the sketch solvable.
Sub Example3dWithObjects()
Dim g As UInteger
Dim slv As New Slvs
' This will contain a single group, which will arbitrarily number 1.
g = 1
Dim p1, p2 As Slvs.Point3d
' A point, initially at (x y z) = (10 10 10)
p1 = slv.NewPoint3d(g, 10.0, 10.0, 10.0)
' and a second point at (20 20 20)
p2 = slv.NewPoint3d(g, 20.0, 20.0, 20.0)
Dim ln As Slvs.LineSegment
' and a line segment connecting them.
ln = slv.NewLineSegment(g, slv.FreeIn3d(), p1, p2)
' The distance between the points should be 30.0 units.
slv.AddConstraint(1, g, Slvs.SLVS_C_PT_PT_DISTANCE, _
slv.FreeIn3d(), 30.0, p1, p2, Nothing, Nothing)
' Let's tell the solver to keep the second point as close to constant
' as possible, instead moving the first point.
slv.Solve(g, p2, True)
If (slv.GetResult() = Slvs.SLVS_RESULT_OKAY) Then
' We call the GetX(), GetY(), and GetZ() functions to see
' where the solver moved our points to.
Console.WriteLine(String.Format( _
"okay; now at ({0:F3}, {1:F3}, {2:F3})", _
p1.GetX(), p1.GetY(), p1.GetZ()))
Console.WriteLine(String.Format( _
" ({0:F3}, {1:F3}, {2:F3})", _
p2.GetX(), p2.GetY(), p2.GetZ()))
Console.WriteLine(slv.GetDof().ToString() + " DOF")
Else
Console.WriteLine("solve failed")
End If
End Sub
Sub Example2dWithObjects()
Dim g As UInteger
Dim slv As New Slvs
g = 1
' First, we create our workplane. Its origin corresponds to the origin
' of our base frame (x y z) = (0 0 0)
Dim origin As Slvs.Point3d
origin = slv.NewPoint3d(g, 0.0, 0.0, 0.0)
' and it is parallel to the xy plane, so it has basis vectors (1 0 0)
' and (0 1 0).
Dim normal As Slvs.Normal3d
normal = slv.NewNormal3d(g, 1.0, 0.0, 0.0, _
0.0, 1.0, 0.0)
Dim wrkpl As Slvs.Workplane
wrkpl = slv.NewWorkplane(g, origin, normal)
' Now create a second group. We'll solve group 2, while leaving group 1
' constant; so the workplane that we've created will be locked down,
' and the solver can't move it.
g = 2
' These points are represented by their coordinates (u v) within the
' workplane, so they need only two parameters each.
Dim pl1, pl2 As Slvs.Point2d
pl1 = slv.NewPoint2d(g, wrkpl, 10.0, 20.0)
pl2 = slv.NewPoint2d(g, wrkpl, 20.0, 10.0)
' And we create a line segment with those endpoints.
Dim ln As Slvs.LineSegment
ln = slv.NewLineSegment(g, wrkpl, pl1, pl2)
' Now three more points.
Dim pc, ps, pf As Slvs.Point2d
pc = slv.NewPoint2d(g, wrkpl, 100.0, 120.0)
ps = slv.NewPoint2d(g, wrkpl, 120.0, 110.0)
pf = slv.NewPoint2d(g, wrkpl, 115.0, 115.0)
' And arc, centered at point pc, starting at point ps, ending at
' point pf.
Dim arc As Slvs.ArcOfCircle
arc = slv.NewArcOfCircle(g, wrkpl, normal, pc, ps, pf)
' Now one more point, and a distance
Dim pcc As Slvs.Point2d
pcc = slv.NewPoint2d(g, wrkpl, 200.0, 200.0)
Dim r As Slvs.Distance
r = slv.NewDistance(g, wrkpl, 30.0)
' And a complete circle, centered at point pcc with radius equal to
' distance r. The normal is the same as for our workplane.
Dim circle As Slvs.Circle
circle = slv.NewCircle(g, wrkpl, pcc, normal, r)
' The length of our line segment is 30.0 units.
slv.AddConstraint(1, g, Slvs.SLVS_C_PT_PT_DISTANCE, _
wrkpl, 30.0, pl1, pl2, Nothing, Nothing)
' And the distance from our line segment to the origin is 10.0 units.
slv.AddConstraint(2, g, Slvs.SLVS_C_PT_LINE_DISTANCE, _
wrkpl, 10.0, origin, Nothing, ln, Nothing)
' And the line segment is vertical.
slv.AddConstraint(3, g, Slvs.SLVS_C_VERTICAL, _
wrkpl, 0.0, Nothing, Nothing, ln, Nothing)
' And the distance from one endpoint to the origin is 15.0 units.
slv.AddConstraint(4, g, Slvs.SLVS_C_PT_PT_DISTANCE, _
wrkpl, 15.0, pl1, origin, Nothing, Nothing)
' And same for the other endpoint; so if you add this constraint then
' the sketch is overconstrained and will signal an error.
'slv.AddConstraint(5, g, Slvs.SLVS_C_PT_PT_DISTANCE, _
' wrkpl, 18.0, pl2, origin, Nothing, Nothing)
' The arc and the circle have equal radius.
slv.AddConstraint(6, g, Slvs.SLVS_C_EQUAL_RADIUS, _
wrkpl, 0.0, Nothing, Nothing, arc, circle)
' The arc has radius 17.0 units.
slv.AddConstraint(7, g, Slvs.SLVS_C_DIAMETER, _
wrkpl, 2 * 17.0, Nothing, Nothing, arc, Nothing)
' If the solver fails, then ask it to report which constraints caused
' the problem.
slv.Solve(g, True)
If (slv.GetResult() = Slvs.SLVS_RESULT_OKAY) Then
Console.WriteLine("solved okay")
' We call the GetU(), GetV(), and GetDistance() functions to
' see where the solver moved our points and distances to.
Console.WriteLine(String.Format( _
"line from ({0:F3} {1:F3}) to ({2:F3} {3:F3})", _
pl1.GetU(), pl1.GetV(), _
pl2.GetU(), pl2.GetV()))
Console.WriteLine(String.Format( _
"arc center ({0:F3} {1:F3}) start ({2:F3} {3:F3}) " + _
"finish ({4:F3} {5:F3})", _
pc.GetU(), pc.GetV(), _
ps.GetU(), ps.GetV(), _
pf.GetU(), pf.GetV()))
Console.WriteLine(String.Format( _
"circle center ({0:F3} {1:F3}) radius {2:F3}", _
pcc.GetU(), pcc.GetV(), _
r.GetDistance()))
Console.WriteLine(slv.GetDof().ToString() + " DOF")
Else
Console.Write("solve failed; problematic constraints are:")
Dim t As UInteger
For Each t In slv.GetFaileds()
Console.Write(" " + t.ToString())
Next
Console.WriteLine("")
If (slv.GetResult() = Slvs.SLVS_RESULT_INCONSISTENT) Then
Console.WriteLine("system inconsistent")
Else
Console.WriteLine("system nonconvergent")
End If
End If
End Sub
'''''''''''''''''''''''''''''''
' This is a lower-level way to use the library. Internally, the library
' represents parameters, entities, and constraints by integer handles.
' Here, those handles are assigned manually, and not by the wrapper
' classes.
Sub Example3dWithHandles()
Dim g As UInteger
Dim slv As New Slvs
' This will contain a single group, which will arbitrarily number 1.
g = 1
' A point, initially at (x y z) = (10 10 10)
slv.AddParam(1, g, 10.0)
slv.AddParam(2, g, 10.0)
slv.AddParam(3, g, 10.0)
slv.AddPoint3d(101, g, 1, 2, 3)
' and a second point at (20 20 20)
slv.AddParam(4, g, 20.0)
slv.AddParam(5, g, 20.0)
slv.AddParam(6, g, 20.0)
slv.AddPoint3d(102, g, 4, 5, 6)
' and a line segment connecting them.
slv.AddLineSegment(200, g, Slvs.SLVS_FREE_IN_3D, 101, 102)
' The distance between the points should be 30.0 units.
slv.AddConstraint(1, g, Slvs.SLVS_C_PT_PT_DISTANCE, _
Slvs.SLVS_FREE_IN_3D, 30.0, 101, 102, 0, 0)
' Let's tell the solver to keep the second point as close to constant
' as possible, instead moving the first point. That's parameters
' 4, 5, and 6.
slv.Solve(g, 4, 5, 6, 0, True)
If (slv.GetResult() = Slvs.SLVS_RESULT_OKAY) Then
' Note that we are referring to the parameters by their handles,
' and not by their index in the list. This is a difference from
' the C example.
Console.WriteLine(String.Format( _
"okay; now at ({0:F3}, {1:F3}, {2:F3})", _
slv.GetParamByHandle(1), slv.GetParamByHandle(2), _
slv.GetParamByHandle(3)))
Console.WriteLine(String.Format( _
" ({0:F3}, {1:F3}, {2:F3})", _
slv.GetParamByHandle(4), slv.GetParamByHandle(5), _
slv.GetParamByHandle(6)))
Console.WriteLine(slv.GetDof().ToString() + " DOF")
Else
Console.WriteLine("solve failed")
End If
End Sub
Sub Example2dWithHandles()
Dim g As UInteger
Dim qw, qx, qy, qz As Double
Dim slv As New Slvs
g = 1
' First, we create our workplane. Its origin corresponds to the origin
' of our base frame (x y z) = (0 0 0)
slv.AddParam(1, g, 0.0)
slv.AddParam(2, g, 0.0)
slv.AddParam(3, g, 0.0)
slv.AddPoint3d(101, g, 1, 2, 3)
' and it is parallel to the xy plane, so it has basis vectors (1 0 0)
' and (0 1 0).
slv.MakeQuaternion(1, 0, 0, _
0, 1, 0, qw, qx, qy, qz)
slv.AddParam(4, g, qw)
slv.AddParam(5, g, qx)
slv.AddParam(6, g, qy)
slv.AddParam(7, g, qz)
slv.AddNormal3d(102, g, 4, 5, 6, 7)
slv.AddWorkplane(200, g, 101, 102)
' Now create a second group. We'll solve group 2, while leaving group 1
' constant; so the workplane that we've created will be locked down,
' and the solver can't move it.
g = 2
' These points are represented by their coordinates (u v) within the
' workplane, so they need only two parameters each.
slv.AddParam(11, g, 10.0)
slv.AddParam(12, g, 20.0)
slv.AddPoint2d(301, g, 200, 11, 12)
slv.AddParam(13, g, 20.0)
slv.AddParam(14, g, 10.0)
slv.AddPoint2d(302, g, 200, 13, 14)
' And we create a line segment with those endpoints.
slv.AddLineSegment(400, g, 200, 301, 302)
' Now three more points.
slv.AddParam(15, g, 100.0)
slv.AddParam(16, g, 120.0)
slv.AddPoint2d(303, g, 200, 15, 16)
slv.AddParam(17, g, 120.0)
slv.AddParam(18, g, 110.0)
slv.AddPoint2d(304, g, 200, 17, 18)
slv.AddParam(19, g, 115.0)
slv.AddParam(20, g, 115.0)
slv.AddPoint2d(305, g, 200, 19, 20)
' And arc, centered at point 303, starting at point 304, ending at
' point 305.
slv.AddArcOfCircle(401, g, 200, 102, 303, 304, 305)
' Now one more point, and a distance
slv.AddParam(21, g, 200.0)
slv.AddParam(22, g, 200.0)
slv.AddPoint2d(306, g, 200, 21, 22)
slv.AddParam(23, g, 30.0)
slv.AddDistance(307, g, 200, 23)
' And a complete circle, centered at point 306 with radius equal to
' distance 307. The normal is 102, the same as our workplane.
slv.AddCircle(402, g, 200, 306, 102, 307)
' The length of our line segment is 30.0 units.
slv.AddConstraint(1, g, Slvs.SLVS_C_PT_PT_DISTANCE, _
200, 30.0, 301, 302, 0, 0)
' And the distance from our line segment to the origin is 10.0 units.
slv.AddConstraint(2, g, Slvs.SLVS_C_PT_LINE_DISTANCE, _
200, 10.0, 101, 0, 400, 0)
' And the line segment is vertical.
slv.AddConstraint(3, g, Slvs.SLVS_C_VERTICAL, _
200, 0.0, 0, 0, 400, 0)
' And the distance from one endpoint to the origin is 15.0 units.
slv.AddConstraint(4, g, Slvs.SLVS_C_PT_PT_DISTANCE, _
200, 15.0, 301, 101, 0, 0)
' And same for the other endpoint; so if you add this constraint then
' the sketch is overconstrained and will signal an error.
'slv.AddConstraint(5, g, Slvs.SLVS_C_PT_PT_DISTANCE, _
' 200, 18.0, 302, 101, 0, 0)
' The arc and the circle have equal radius.
slv.AddConstraint(6, g, Slvs.SLVS_C_EQUAL_RADIUS, _
200, 0.0, 0, 0, 401, 402)
' The arc has radius 17.0 units.
slv.AddConstraint(7, g, Slvs.SLVS_C_DIAMETER, _
200, 2 * 17.0, 0, 0, 401, 0)
' If the solver fails, then ask it to report which constraints caused
' the problem.
slv.Solve(g, 0, 0, 0, 0, True)
If (slv.GetResult() = Slvs.SLVS_RESULT_OKAY) Then
Console.WriteLine("solved okay")
' Note that we are referring to the parameters by their handles,
' and not by their index in the list. This is a difference from
' the C example.
Console.WriteLine(String.Format( _
"line from ({0:F3} {1:F3}) to ({2:F3} {3:F3})", _
slv.GetParamByHandle(11), slv.GetParamByHandle(12), _
slv.GetParamByHandle(13), slv.GetParamByHandle(14)))
Console.WriteLine(String.Format( _
"arc center ({0:F3} {1:F3}) start ({2:F3} {3:F3}) " + _
"finish ({4:F3} {5:F3})", _
slv.GetParamByHandle(15), slv.GetParamByHandle(16), _
slv.GetParamByHandle(17), slv.GetParamByHandle(18), _
slv.GetParamByHandle(19), slv.GetParamByHandle(20)))
Console.WriteLine(String.Format( _
"circle center ({0:F3} {1:F3}) radius {2:F3}", _
slv.GetParamByHandle(21), slv.GetParamByHandle(22), _
slv.GetParamByHandle(23)))
Console.WriteLine(slv.GetDof().ToString() + " DOF")
Else
Console.Write("solve failed; problematic constraints are:")
Dim t As UInteger
For Each t In slv.GetFaileds()
Console.Write(" " + t.ToString())
Next
Console.WriteLine("")
If (slv.GetResult() = Slvs.SLVS_RESULT_INCONSISTENT) Then
Console.WriteLine("system inconsistent")
Else
Console.WriteLine("system nonconvergent")
End If
End If
End Sub
'''''''''''''''''''''''''''''''
' The interface to the library, and the wrapper functions around
' that interface, follow.
' These are the core functions imported from the DLL
<DllImport("slvs.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Sub Slvs_Solve(ByVal sys As IntPtr, ByVal hg As UInteger)
End Sub
<DllImport("slvs.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Sub Slvs_MakeQuaternion(
ByVal ux As Double, ByVal uy As Double, ByVal uz As Double,
ByVal vx As Double, ByVal vy As Double, ByVal vz As Double,
ByRef qw As Double, ByRef qx As Double, ByRef qy As Double,
ByRef qz As Double)
End Sub
' And this is a thin wrapper around those functions, which provides
' convenience functions similar to those provided in slvs.h for the C API.
Public Class Slvs
<StructLayout(LayoutKind.Sequential)> Public Structure Slvs_Param
Public h As UInteger
Public group As UInteger
Public val As Double
End Structure
Public Const SLVS_FREE_IN_3D As Integer = 0
Public Const SLVS_E_POINT_IN_3D As Integer = 50000
Public Const SLVS_E_POINT_IN_2D As Integer = 50001
Public Const SLVS_E_NORMAL_IN_3D As Integer = 60000
Public Const SLVS_E_NORMAL_IN_2D As Integer = 60001
Public Const SLVS_E_DISTANCE As Integer = 70000
Public Const SLVS_E_WORKPLANE As Integer = 80000
Public Const SLVS_E_LINE_SEGMENT As Integer = 80001
Public Const SLVS_E_CUBIC As Integer = 80002
Public Const SLVS_E_CIRCLE As Integer = 80003
Public Const SLVS_E_ARC_OF_CIRCLE As Integer = 80004
<StructLayout(LayoutKind.Sequential)> Public Structure Slvs_Entity
Public h As UInteger
Public group As UInteger
Public type As Integer
Public wrkpl As UInteger
Public point0 As UInteger
Public point1 As UInteger
Public point2 As UInteger
Public point3 As UInteger
Public normal As UInteger
Public distance As UInteger
Public param0 As UInteger
Public param1 As UInteger
Public param2 As UInteger
Public param3 As UInteger
End Structure
Public Const SLVS_C_POINTS_COINCIDENT As Integer = 100000
Public Const SLVS_C_PT_PT_DISTANCE As Integer = 100001
Public Const SLVS_C_PT_PLANE_DISTANCE As Integer = 100002
Public Const SLVS_C_PT_LINE_DISTANCE As Integer = 100003
Public Const SLVS_C_PT_FACE_DISTANCE As Integer = 100004
Public Const SLVS_C_PT_IN_PLANE As Integer = 100005
Public Const SLVS_C_PT_ON_LINE As Integer = 100006
Public Const SLVS_C_PT_ON_FACE As Integer = 100007
Public Const SLVS_C_EQUAL_LENGTH_LINES As Integer = 100008
Public Const SLVS_C_LENGTH_RATIO As Integer = 100009
Public Const SLVS_C_EQ_LEN_PT_LINE_D As Integer = 100010
Public Const SLVS_C_EQ_PT_LN_DISTANCES As Integer = 100011
Public Const SLVS_C_EQUAL_ANGLE As Integer = 100012
Public Const SLVS_C_EQUAL_LINE_ARC_LEN As Integer = 100013
Public Const SLVS_C_SYMMETRIC As Integer = 100014
Public Const SLVS_C_SYMMETRIC_HORIZ As Integer = 100015
Public Const SLVS_C_SYMMETRIC_VERT As Integer = 100016
Public Const SLVS_C_SYMMETRIC_LINE As Integer = 100017
Public Const SLVS_C_AT_MIDPOINT As Integer = 100018
Public Const SLVS_C_HORIZONTAL As Integer = 100019
Public Const SLVS_C_VERTICAL As Integer = 100020
Public Const SLVS_C_DIAMETER As Integer = 100021
Public Const SLVS_C_PT_ON_CIRCLE As Integer = 100022
Public Const SLVS_C_SAME_ORIENTATION As Integer = 100023
Public Const SLVS_C_ANGLE As Integer = 100024
Public Const SLVS_C_PARALLEL As Integer = 100025
Public Const SLVS_C_PERPENDICULAR As Integer = 100026
Public Const SLVS_C_ARC_LINE_TANGENT As Integer = 100027
Public Const SLVS_C_CUBIC_LINE_TANGENT As Integer = 100028
Public Const SLVS_C_EQUAL_RADIUS As Integer = 100029
Public Const SLVS_C_PROJ_PT_DISTANCE As Integer = 100030
Public Const SLVS_C_WHERE_DRAGGED As Integer = 100031
Public Const SLVS_C_CURVE_CURVE_TANGENT As Integer = 100032
Public Const SLVS_C_LENGTH_DIFFERENCE As Integer = 100033
<StructLayout(LayoutKind.Sequential)> Public Structure Slvs_Constraint
Public h As UInteger
Public group As UInteger
Public type As Integer
Public wrkpl As UInteger
Public valA As Double
Public ptA As UInteger
Public ptB As UInteger
Public entityA As UInteger
Public entityB As UInteger
Public entityC As UInteger
Public entityD As UInteger
Public other As Integer
Public other2 As Integer
End Structure
Public Const SLVS_RESULT_OKAY As Integer = 0
Public Const SLVS_RESULT_INCONSISTENT As Integer = 1
Public Const SLVS_RESULT_DIDNT_CONVERGE As Integer = 2
Public Const SLVS_RESULT_TOO_MANY_UNKNOWNS As Integer = 3
<StructLayout(LayoutKind.Sequential)> Public Structure Slvs_System
Public param As IntPtr
Public params As Integer
Public entity As IntPtr
Public entities As Integer
Public constraint As IntPtr
Public constraints As Integer
Public dragged0 As UInteger
Public dragged1 As UInteger
Public dragged2 As UInteger
Public dragged3 As UInteger
Public calculatedFaileds As Integer
Public failed As IntPtr
Public faileds As Integer
Public dof As Integer
Public result As Integer
End Structure
Dim Params As New List(Of Slvs_Param)
Dim Entities As New List(Of Slvs_Entity)
Dim Constraints As New List(Of Slvs_Constraint)
Dim Faileds As New List(Of UInteger)
Dim Result As Integer
Dim Dof As Integer
' Return the value of a parameter, by its handle. This function
' may be used, for example, to obtain the new values of the
' parameters after a call to Solve().
Public Function GetParamByHandle(ByVal h As UInteger) As Double
Dim t As Slvs_Param
For Each t In Params
If (t.h = h) Then
Return t.val
End If
Next
Throw New Exception("Invalid parameter handle.")
End Function
' Return the value of a parameter, by its index in the list (where
' that index is determined by the order in which the parameters
' were inserted with AddParam(), not by its handle).
Public Function GetParamByIndex(ByVal i As Integer) As Double
Return Params(i).val
End Function
' Get the result after a call to Solve(). This may be
' SLVS_RESULT_OKAY - it worked
' SLVS_RESULT_INCONSISTENT - failed, inconsistent
' SLVS_RESULT_DIDNT_CONVERGE - consistent, but still failed
' SLVS_RESULT_TOO_MANY_UNKNOWNS - too many parameters in one group
Public Function GetResult() As Integer
Return Result
End Function
' After a call to Solve(), this returns the number of unconstrained
' degrees of freedom for the sketch.
Public Function GetDof() As Integer
Return Dof
End Function
' After a failing call to Solve(), this returns the list of
' constraints, identified by their handle, that would fix the
' system if they were deleted. This list will be populated only
' if calculateFaileds is True in the Solve() call.
Public Function GetFaileds() As List(Of UInteger)
Return Faileds
End Function
' Clear our lists of entities, constraints, and parameters.
Public Sub ResetAll()
Params.Clear()
Entities.Clear()
Constraints.Clear()
End Sub
'''''''''''''''''''''''''''''''
' These functions are broadly similar to the Slvs_Make...
' functions in slvs.h. See the file DOC.txt accompanying the
' library for details.
Public Sub AddParam(ByVal h As UInteger, ByVal group As UInteger,
ByVal val As Double)
Dim p As Slvs_Param
p.h = h
p.group = group
p.val = val
Params.Add(p)
End Sub
Public Sub AddPoint2d(ByVal h As UInteger, ByVal group As UInteger,
ByVal wrkpl As UInteger,
ByVal u As UInteger, ByVal v As UInteger)
Dim e As Slvs_Entity
e.h = h
e.group = group
e.type = SLVS_E_POINT_IN_2D
e.wrkpl = wrkpl
e.param0 = u
e.param1 = v
Entities.Add(e)
End Sub
Public Sub AddPoint3d(ByVal h As UInteger, ByVal group As UInteger,
ByVal x As UInteger, ByVal y As UInteger, ByVal z As UInteger)
Dim e As Slvs_Entity
e.h = h
e.group = group
e.type = SLVS_E_POINT_IN_3D
e.wrkpl = SLVS_FREE_IN_3D
e.param0 = x
e.param1 = y
e.param2 = z
Entities.Add(e)
End Sub
Public Sub AddNormal3d(ByVal h As UInteger, ByVal group As UInteger,
ByVal qw As UInteger, ByVal qx As UInteger,
ByVal qy As UInteger, ByVal qz As UInteger)
Dim e As Slvs_Entity
e.h = h
e.group = group
e.type = SLVS_E_NORMAL_IN_3D
e.wrkpl = SLVS_FREE_IN_3D
e.param0 = qw
e.param1 = qx
e.param2 = qy
e.param3 = qz
Entities.Add(e)
End Sub
Public Sub AddNormal2d(ByVal h As UInteger, ByVal group As UInteger,
ByVal wrkpl As UInteger)
Dim e As Slvs_Entity
e.h = h
e.group = group
e.type = SLVS_E_NORMAL_IN_2D
e.wrkpl = wrkpl
Entities.Add(e)
End Sub
Public Sub AddDistance(ByVal h As UInteger, ByVal group As UInteger,
ByVal wrkpl As UInteger, ByVal d As UInteger)
Dim e As Slvs_Entity
e.h = h
e.group = group
e.type = SLVS_E_DISTANCE
e.wrkpl = wrkpl
e.param0 = d
Entities.Add(e)
End Sub
Public Sub AddLineSegment(ByVal h As UInteger, ByVal group As UInteger,
ByVal wrkpl As UInteger,
ByVal ptA As UInteger, ByVal ptB As UInteger)
Dim e As Slvs_Entity
e.h = h
e.group = group
e.type = SLVS_E_LINE_SEGMENT
e.wrkpl = wrkpl
e.point0 = ptA
e.point1 = ptB
Entities.Add(e)
End Sub
Public Sub AddCubic(ByVal h As UInteger, ByVal group As UInteger,
ByVal wrkpl As UInteger,
ByVal pt0 As UInteger, ByVal pt1 As UInteger,
ByVal pt2 As UInteger, ByVal pt3 As UInteger)
Dim e As Slvs_Entity
e.h = h
e.group = group
e.type = SLVS_E_CUBIC
e.wrkpl = wrkpl
e.point0 = pt0
e.point1 = pt1
e.point2 = pt2
e.point3 = pt3
Entities.Add(e)
End Sub
Public Sub AddArcOfCircle(ByVal h As UInteger, ByVal group As UInteger,
ByVal wrkpl As UInteger,
ByVal normal As UInteger,
ByVal center As UInteger,
ByVal pstart As UInteger,
ByVal pend As UInteger)
Dim e As Slvs_Entity
e.h = h
e.group = group
e.type = SLVS_E_ARC_OF_CIRCLE
e.wrkpl = wrkpl
e.normal = normal
e.point0 = center
e.point1 = pstart
e.point2 = pend
Entities.Add(e)
End Sub
Public Sub AddCircle(ByVal h As UInteger, ByVal group As UInteger,
ByVal wrkpl As UInteger,
ByVal center As UInteger, ByVal normal As UInteger,
ByVal radius As UInteger)
Dim e As Slvs_Entity
e.h = h
e.group = group
e.type = SLVS_E_CIRCLE
e.wrkpl = wrkpl
e.point0 = center
e.normal = normal
e.distance = radius
Entities.Add(e)
End Sub
Public Sub AddWorkplane(ByVal h As UInteger, ByVal group As UInteger,
ByVal origin As UInteger,
ByVal normal As UInteger)
Dim e As Slvs_Entity
e.h = h
e.group = group
e.type = SLVS_E_WORKPLANE
e.wrkpl = SLVS_FREE_IN_3D
e.point0 = origin
e.normal = normal
Entities.Add(e)
End Sub
Public Sub AddConstraint(ByVal h As UInteger,
ByVal group As UInteger,
ByVal type As Integer,
ByVal wrkpl As UInteger,
ByVal valA As Double,
ByVal ptA As UInteger,
ByVal ptB As UInteger,
ByVal entityA As UInteger,
ByVal entityB As UInteger)
Dim c As Slvs_Constraint
c.h = h
c.group = group
c.type = type
c.wrkpl = wrkpl
c.valA = valA
c.ptA = ptA
c.ptB = ptB
c.entityA = entityA
c.entityB = entityB
Constraints.Add(c)
End Sub
' Solve the system. The geometry of the system must already have
' been specified through the Add...() calls. The result of the
' solution process may be obtained by calling GetResult(),
' GetFaileds(), GetDof(), and GetParamByXXX().
'
' The parameters draggedx (indicated by their handles) will be held
' as close as possible to their original positions, even if this
' results in large moves for other parameters. This feature may be
' useful if, for example, the user is dragging the point whose
' location is defined by those parameters. Unused draggedx
' parameters may be specified as zero.
Public Sub Solve(ByVal group As UInteger,
ByVal dragged0 As UInteger, ByVal dragged1 As UInteger,
ByVal dragged2 As UInteger, ByVal dragged3 As UInteger,
ByVal calculateFaileds As Boolean)
Dim i As Integer
Dim pp, p(Params.Count()) As Slvs_Param
i = 0
For Each pp In Params
p(i) = pp
i += 1
Next
Dim ee, e(Entities.Count()) As Slvs_Entity
i = 0
For Each ee In Entities
e(i) = ee
i += 1
Next
Dim cc, c(Constraints.Count()) As Slvs_Constraint
i = 0
For Each cc In Constraints
c(i) = cc
i += 1
Next
Dim f(Constraints.Count()) As UInteger
Dim sys As Slvs_System
Dim pgc, egc, cgc As GCHandle
pgc = GCHandle.Alloc(p, GCHandleType.Pinned)
sys.param = pgc.AddrOfPinnedObject()
sys.params = Params.Count()
egc = GCHandle.Alloc(e, GCHandleType.Pinned)
sys.entity = egc.AddrOfPinnedObject()
sys.entities = Entities.Count()
cgc = GCHandle.Alloc(c, GCHandleType.Pinned)
sys.constraint = cgc.AddrOfPinnedObject()
sys.constraints = Constraints.Count()
sys.dragged0 = dragged0
sys.dragged1 = dragged1
sys.dragged2 = dragged2
sys.dragged3 = dragged3
Dim fgc As GCHandle
fgc = GCHandle.Alloc(f, GCHandleType.Pinned)
If calculateFaileds Then
sys.calculatedFaileds = 1
Else
sys.calculatedFaileds = 0
End If
sys.faileds = Constraints.Count()
sys.failed = fgc.AddrOfPinnedObject()
Dim sysgc As GCHandle
sysgc = GCHandle.Alloc(sys, GCHandleType.Pinned)
Slvs_Solve(sysgc.AddrOfPinnedObject(), group)
sys = sysgc.Target
For i = 0 To Params.Count() - 1
Params(i) = p(i)
Next
Faileds.Clear()
For i = 0 To sys.faileds - 1
Faileds.Add(f(i))
Next
sysgc.Free()
fgc.Free()
pgc.Free()
egc.Free()
cgc.Free()
Result = sys.result
Dof = sys.dof
End Sub
' A simpler version of the function, if the parameters being dragged
' correspond to a single point.
Public Sub Solve(ByVal group As UInteger, ByVal dragged As Point,
ByVal calculatedFaileds As Boolean)
If TypeOf dragged Is Point2d Then
Dim p As Point2d
p = dragged
Solve(group, p.up.H, p.vp.H, 0, 0, calculatedFaileds)
ElseIf TypeOf dragged Is Point3d Then
Dim p As Point3d
p = dragged
Solve(group, p.xp.H, p.yp.H, p.zp.H, 0, calculatedFaileds)
Else
Throw New Exception("Can't get dragged params for point.")
End If
End Sub
' or if it's a single distance (e.g., the radius of a circle)
Public Sub Solve(ByVal group As UInteger, ByVal dragged As Distance,
ByVal calculatedFaileds As Boolean)
Solve(group, dragged.dp.H, 0, 0, 0, calculatedFaileds)
End Sub
' or if it's nothing.
Public Sub Solve(ByVal group As UInteger,
ByVal calculateFaileds As Boolean)
Solve(group, 0, 0, 0, 0, calculateFaileds)
End Sub
' Return the quaternion in (qw, qx, qy, qz) that represents a
' rotation from the base frame to a coordinate system with the
' specified basis vectors u and v. For example, u = (0, 1, 0)
' and v = (0, 0, 1) specifies the yz plane, such that a point with
' (u, v) = (7, 12) has (x, y, z) = (0, 7, 12).
Public Sub MakeQuaternion(
ByVal ux As Double, ByVal uy As Double, ByVal uz As Double,
ByVal vx As Double, ByVal vy As Double, ByVal vz As Double,
ByRef qw As Double, ByRef qx As Double, ByRef qy As Double,
ByRef qz As Double)
Slvs_MakeQuaternion(ux, uy, uz, _
vx, vy, vz, _
qw, qx, qy, qz)
End Sub
Public Function FreeIn3d()
Return New Workplane(Me)
End Function
'''''''''''''''''''''''''''''''
' Functions to create the object-oriented wrappers defined below.
Public Function NewParam(ByVal group As UInteger, ByVal val As Double)
Return New Param(Me, group, val)
End Function
Public Function NewPoint2d(ByVal group As UInteger,
ByVal wrkpl As Workplane,
ByVal u As Double, ByVal v As Double)
Return New Point2d(Me, group, wrkpl, u, v)
End Function
Public Function NewPoint2d(ByVal group As UInteger,
ByVal wrkpl As Workplane,
ByVal u As Param, ByVal v As Param)
Return New Point2d(Me, group, wrkpl, u, v)
End Function
Public Function NewPoint3d(ByVal group As UInteger,
ByVal x As Double,
ByVal y As Double,
ByVal z As Double)
Return New Point3d(Me, group, x, y, z)
End Function
Public Function NewPoint3d(ByVal group As UInteger,
ByVal x As Param,
ByVal y As Param,
ByVal z As Param)
Return New Point3d(Me, group, x, y, z)
End Function
Public Function NewNormal3d(ByVal group As UInteger,
ByVal ux As Double, ByVal uy As Double, ByVal uz As Double,
ByVal vx As Double, ByVal vy As Double, ByVal vz As Double)
Return New Normal3d(Me, group, ux, uy, uz, vx, vy, vz)
End Function
Public Function NewNormal3d(ByVal group As UInteger,
ByVal qw As Param, ByVal qx As Param,
ByVal qy As Param, ByVal qz As Param)
Return New Normal3d(Me, group, qw, qx, qy, qz)
End Function
Public Function NewNormal2d(ByVal group As UInteger,
ByVal wrkpl As Workplane)
Return New Normal2d(Me, group, wrkpl)
End Function
Public Function NewDistance(ByVal group As UInteger,
ByVal wrkpl As Workplane, ByVal d As Double)
Return New Distance(Me, group, wrkpl, d)
End Function
Public Function NewDistance(ByVal group As UInteger,
ByVal wrkpl As Workplane, ByVal d As Param)
Return New Distance(Me, group, wrkpl, d)
End Function
Public Function NewLineSegment(ByVal group As UInteger,
ByVal wrkpl As Workplane,
ByVal ptA As Point, ByVal ptB As Point)
Return New LineSegment(Me, group, wrkpl, ptA, ptB)
End Function
Public Function NewArcOfCircle(ByVal group As UInteger,
ByVal wrkpl As Workplane,
ByVal normal As Normal,
ByVal center As Point,
ByVal pstart As Point,
ByVal pend As Point)
Return New ArcOfCircle(Me, group, wrkpl, normal, _
center, pstart, pend)
End Function
Public Function NewCircle(ByVal group As UInteger,