Skip to content

Reference

Bases: PropertyGroup

The base structure for CAD Sketcher

Source code in model/group_sketcher.py
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
class SketcherProps(PropertyGroup):
    """The base structure for CAD Sketcher"""

    entities: PointerProperty(type=SlvsEntities)
    constraints: PointerProperty(type=SlvsConstraints)
    show_origin: BoolProperty(name="Show Origin Entities")
    use_construction: BoolProperty(
        name="Construction Mode",
        description="Draw all subsequent entities in construction mode",
        default=False,
        options={"SKIP_SAVE"},
        update=update_cb,
    )
    selectable_constraints: BoolProperty(
        name="Constraints Selectability",
        default=True,
        options={"SKIP_SAVE"},
        update=update_cb,
    )

    version: IntVectorProperty(
        name="Extension Version",
        description="CAD Sketcher extension version this scene was saved with",
    )

    # This is needed for the sketches ui list
    ui_active_sketch: IntProperty()

    @property
    def all(self) -> Generator[Union[SlvsGenericEntity, SlvsConstraints], None, None]:
        """Iterate over entities and constraints of every type"""
        for entity in self.entities.all:
            yield entity
        for constraint in self.constraints.all:
            yield constraint

    def solve(self, context: Context):
        return solve_system(context)

    def purge_stale_data(self):
        global_data.hover = -1
        global_data.selected.clear()
        global_data.batches.clear()
        for e in self.entities.all:
            e.dirty = True

all: Generator[Union[SlvsGenericEntity, SlvsConstraints], None, None] property

Iterate over entities and constraints of every type

Bases: PropertyGroup

Holds all Solvespace Entities

Source code in model/group_entities.py
 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
class SlvsEntities(PropertyGroup):
    """Holds all Solvespace Entities"""

    @classmethod
    def _type_index(cls, entity: SlvsGenericEntity) -> int:
        return _entity_types.index(type(entity))

    def _set_index(self, entity: SlvsGenericEntity):
        """Create an index for the entity and assign it.
        Index breakdown

        | entity type index |  entity object index  |
        |:-----------------:|:---------------------:|
        |      4 bits       |       20 bits         |
        |            total: 3 Bytes                 |
        """
        type_index = self._type_index(entity)
        sub_list = getattr(self, _entity_collections[type_index])

        local_index = len(sub_list) - 1
        # TODO: handle this case better
        assert local_index < math.pow(2, 20)
        index = assemble_index(type_index, local_index)
        entity.slvs_index = index
        return index

    @staticmethod
    def _breakdown_index(index: int):
        return breakdown_index(index)

    @classmethod
    def recalc_type_index(cls, entity):
        _, local_index = cls._breakdown_index(entity.slvs_index)
        type_index = cls._type_index(entity)
        entity.slvs_index = type_index << 20 | local_index

    def type_from_index(self, index: int) -> Type[SlvsGenericEntity]:
        return type_from_index(index)

    def collection_name_from_index(self, index: int):
        if index < 0:
            return

        type_index, _ = self._breakdown_index(index)
        return _entity_collections[type_index]

    def _get_list_and_index(self, index: int):
        type_index, local_index = self._breakdown_index(index)
        if type_index < 0 or type_index >= len(_entity_collections):
            return None, local_index
        return getattr(self, _entity_collections[type_index]), local_index

    def get(self, index: int) -> SlvsGenericEntity:
        """Get entity by index

        Arguments:
            index: The global index of the entity.

        Returns:
            SlvsGenericEntity: Entity with the given global index or None if not found.
        """
        if index == -1:
            return None
        sub_list, i = self._get_list_and_index(index)
        if not sub_list or i >= len(sub_list):
            return None
        return sub_list[i]

    def remove(self, index: int):
        """Remove entity by index

        Arguments:
            index: The global index of the entity.
        """
        assert isinstance(index, int)

        if self.get(index).origin:
            return

        entity_list, i = self._get_list_and_index(index)
        entity_list.remove(i)

        # Put last item to removed index and update all pointers to it
        last_index = len(entity_list) - 1

        if last_index < 0:
            return
        if i > last_index:
            return

        if not i == last_index:  # second last item was deleted
            entity_list.move(last_index, i)

        new_item = entity_list[i]
        update_pointers(bpy.context.scene, new_item.slvs_index, index)
        new_item.slvs_index = index

    def _init_entity(self, entity, fixed, construction, index_reference, visible=True):
        """Initializes all shared entity properties"""

        entity["fixed"] = fixed
        entity["construction"] = construction
        entity["visible"] = visible

        index = self._set_index(entity)

        if index_reference:
            return index
        return entity

    def add_point_3d(
        self,
        co: Union[Tuple[float, float, float], Vector],
        fixed: bool = False,
        construction: bool = False,
        index_reference: bool = False,
    ) -> Union[SlvsPoint3D, int]:
        """Add a point in 3d space.

        Arguments:
            co: Location of the point in 3d space.

        Returns:
            SlvsPoint3D: The created point.
        """
        if not hasattr(co, "__len__") or len(co) != 3:
            raise TypeError("Argument co must be of length 3")

        p = self.points3D.add()
        p["location"] = Vector(co)
        return self._init_entity(p, fixed, construction, index_reference)

    def add_line_3d(
        self,
        p1: Union[SlvsPoint3D, int],
        p2: Union[SlvsPoint3D, int],
        fixed: bool = False,
        construction: bool = False,
        index_reference: bool = False,
    ) -> SlvsLine3D:
        """Add a line in 3d space.

        Arguments:
            p1: Line's startpoint.
            p2: Line's endpoint.

        Returns:
            SlvsLine3D: The created line.
        """
        line = self.lines3D.add()
        line["p1_i"] = p1 if isinstance(p1, int) else p1.slvs_index
        line["p2_i"] = p2 if isinstance(p2, int) else p2.slvs_index

        return self._init_entity(line, fixed, construction, index_reference)

    def add_normal_3d(
        self,
        quat: Tuple[float, float, float, float],
        fixed: bool = False,
        construction: bool = False,
        index_reference: bool = False,
    ) -> SlvsNormal3D:
        """Add a normal in 3d space.

        Arguments:
            quat: Quaternion which describes the orientation.

        Returns:
            SlvsNormal3D: The created normal.
        """
        nm = self.normals3D.add()
        nm["orientation"] = Quaternion(quat)

        return self._init_entity(nm, fixed, construction, index_reference)

    def add_workplane(
        self,
        p1: SlvsPoint3D,
        nm: SlvsGenericEntity,
        fixed: bool = False,
        construction: bool = False,
        index_reference: bool = False,
    ) -> SlvsWorkplane:
        """Add a workplane.

        Arguments:
            p1: Workplane's originpoint.
            nm: Workplane's normal.

        Returns:
            SlvsWorkplane: The created workplane.
        """
        wp = self.workplanes.add()
        wp["p1_i"] = p1 if isinstance(p1, int) else p1.slvs_index
        wp["nm_i"] = nm if isinstance(nm, int) else nm.slvs_index

        return self._init_entity(wp, fixed, construction, index_reference)

    def add_sketch(
        self,
        wp: SlvsWorkplane,
        fixed: bool = False,
        construction: bool = False,
        index_reference: bool = False,
    ) -> SlvsSketch:
        """Add a Sketch.

        Arguments:
            wp: Sketch's workplane.

        Returns:
            SlvsSketch: The created sketch.
        """
        sketch = self.sketches.add()
        sketch["wp_i"] = wp if isinstance(wp, int) else wp.slvs_index

        retval = self._init_entity(sketch, fixed, construction, index_reference)
        index = retval if index_reference else retval.slvs_index
        _, i = self._breakdown_index(index)
        sketch.name = "Sketch"
        return retval

    def add_point_2d(
        self,
        co: Tuple[float, float],
        sketch: SlvsSketch,
        fixed: bool = False,
        construction: bool = False,
        index_reference: bool = False,
    ) -> SlvsPoint2D:
        """Add a point in 2d space.

        Arguments:
            co: Coordinates of the point on the workplane.
            sketch: The sketch this point belongs to.

        Returns:
            SlvsPoint2D: The created point.
        """
        p = self.points2D.add()
        p["co"] = Vector(co)
        p["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index

        return self._init_entity(p, fixed, construction, index_reference)

    def add_line_2d(
        self,
        p1: SlvsPoint2D,
        p2: SlvsPoint2D,
        sketch: SlvsSketch,
        fixed: bool = False,
        construction: bool = False,
        index_reference: bool = False,
    ) -> SlvsLine2D:
        """Add a line in 2d space.

        Arguments:
            p1: Line's startpoint.
            p2: Line's endpoint.
            sketch: The sketch this line belongs to.

        Returns:
            SlvsLine2D: The created line.
        """
        line = self.lines2D.add()
        line["p1_i"] = p1 if isinstance(p1, int) else p1.slvs_index
        line["p2_i"] = p2 if isinstance(p2, int) else p2.slvs_index
        line["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index

        return self._init_entity(line, fixed, construction, index_reference)

    def add_normal_2d(
        self,
        sketch: SlvsSketch,
        fixed: bool = False,
        construction: bool = False,
        index_reference: bool = False,
    ) -> SlvsNormal2D:
        """Add a normal in 2d space.

        Arguments:
            sketch: The sketch this normal belongs to.

        Returns:
            SlvsNormal2D: The created normal.
        """
        nm = self.normals2D.add()
        nm["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index

        return self._init_entity(nm, fixed, construction, index_reference)

    def add_arc(
        self,
        nm: SlvsNormal2D,
        ct: SlvsPoint2D,
        p1: SlvsPoint2D,
        p2: SlvsPoint2D,
        sketch: SlvsSketch,
        invert: bool = False,
        fixed: bool = False,
        construction: bool = False,
        index_reference: bool = False,
    ) -> SlvsArc:
        """Add an arc in 2d space.

        Arguments:
            ct: Arc's centerpoint.
            p1: Arc's startpoint.
            p2: Arc's endpoint.
            sketch: The sketch this arc belongs to.
            nm: Arc's normal.

        Returns:
            SlvsArc: The created arc.
        """
        arc = self.arcs.add()
        arc["nm_i"] = nm if isinstance(nm, int) else nm.slvs_index
        arc["ct_i"] = ct if isinstance(ct, int) else ct.slvs_index
        arc["p1_i"] = p1 if isinstance(p1, int) else p1.slvs_index
        arc["p2_i"] = p2 if isinstance(p2, int) else p2.slvs_index
        arc["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        arc["invert_direction"] = invert

        return self._init_entity(arc, fixed, construction, index_reference)

    def add_circle(
        self,
        nm: SlvsNormal2D,
        ct: SlvsPoint2D,
        radius: float,
        sketch: SlvsSketch,
        fixed: bool = False,
        construction: bool = False,
        index_reference: bool = False,
    ) -> SlvsCircle:
        """Add a circle in 2d space.

        Arguments:
            ct: Circle's centerpoint.
            radius: Circle's radius.
            sketch: The sketch this circle belongs to.
            nm: Circle's normal.

        Returns:
            SlvsCircle: The created circle.
        """
        c = self.circles.add()
        c["nm_i"] = nm if isinstance(nm, int) else nm.slvs_index
        c["ct_i"] = ct if isinstance(ct, int) else ct.slvs_index
        c["radius"] = float(radius)
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index

        return self._init_entity(c, fixed, construction, index_reference)

    @property
    def all(self):
        for coll_name in _entity_collections:
            entity_coll = getattr(self, coll_name)
            for entity in entity_coll:
                yield entity

    @property
    def selected(self):
        """Return all selected entities, might include inactive entities"""
        context = bpy.context
        items = []
        for index in global_data.selected:
            if index is None:
                continue
            entity = self.get(index)
            items.append(entity)
        return [e for e in items if e.is_selectable(context)]

    @property
    def selected_all(self):
        """Return all selected entities, might include invisible entities"""
        context = bpy.context
        items = []
        for index in global_data.selected:
            if index is None:
                continue
            entity = self.get(index)
            items.append(entity)
        return [e for e in items if e.selected]

    @property
    def selected_active(self):
        """Returns all selected and active entities"""
        context = bpy.context
        active_sketch = context.scene.sketcher.active_sketch
        return [e for e in self.selected if e.is_active(active_sketch)]

    def ensure_origin_elements(self, context):
        def set_origin_props(e):
            e.fixed = True
            e.origin = True

        sse = context.scene.sketcher.entities
        # origin
        if not self.origin:
            p = sse.add_point_3d((0.0, 0.0, 0.0))
            set_origin_props(p)
            p.name = "OriginPoint3D"
            self.origin = p

        # axis
        pi_2 = QUARTER_TURN
        for label, name, angles in zip(
            ("OriginAxisX", "OriginAxisY", "OriginAxisZ"),
            ("origin_axis_X", "origin_axis_Y", "origin_axis_Z"),
            (Euler((pi_2, 0.0, pi_2)), Euler((pi_2, 0.0, 0.0)), Euler()),
        ):
            if getattr(self, name):
                continue
            nm = sse.add_normal_3d(Euler(angles).to_quaternion())
            set_origin_props(nm)
            setattr(self, name, nm)
            nm.name = label

        # workplanes
        for label, nm_name, wp_name in (
            ("OriginWorkplaneYZ", "origin_axis_X", "origin_plane_YZ"),
            ("OriginWorkplaneXZ", "origin_axis_Y", "origin_plane_XZ"),
            ("OriginWorkplaneXY", "origin_axis_Z", "origin_plane_XY"),
        ):
            if getattr(self, wp_name):
                continue
            wp = sse.add_workplane(self.origin, getattr(self, nm_name))
            set_origin_props(wp)
            setattr(self, wp_name, wp)
            wp.name = label

    def collection_offsets(self):
        offsets = {}
        for i, key in enumerate(_entity_collections):
            offsets[i] = len(getattr(self, key))
        return offsets

selected property

Return all selected entities, might include inactive entities

selected_active property

Returns all selected and active entities

selected_all property

Return all selected entities, might include invisible entities

add_arc(nm, ct, p1, p2, sketch, invert=False, fixed=False, construction=False, index_reference=False)

Add an arc in 2d space.

Parameters:

Name Type Description Default
ct SlvsPoint2D

Arc's centerpoint.

required
p1 SlvsPoint2D

Arc's startpoint.

required
p2 SlvsPoint2D

Arc's endpoint.

required
sketch SlvsSketch

The sketch this arc belongs to.

required
nm SlvsNormal2D

Arc's normal.

required

Returns:

Name Type Description
SlvsArc SlvsArc

The created arc.

Source code in model/group_entities.py
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
def add_arc(
    self,
    nm: SlvsNormal2D,
    ct: SlvsPoint2D,
    p1: SlvsPoint2D,
    p2: SlvsPoint2D,
    sketch: SlvsSketch,
    invert: bool = False,
    fixed: bool = False,
    construction: bool = False,
    index_reference: bool = False,
) -> SlvsArc:
    """Add an arc in 2d space.

    Arguments:
        ct: Arc's centerpoint.
        p1: Arc's startpoint.
        p2: Arc's endpoint.
        sketch: The sketch this arc belongs to.
        nm: Arc's normal.

    Returns:
        SlvsArc: The created arc.
    """
    arc = self.arcs.add()
    arc["nm_i"] = nm if isinstance(nm, int) else nm.slvs_index
    arc["ct_i"] = ct if isinstance(ct, int) else ct.slvs_index
    arc["p1_i"] = p1 if isinstance(p1, int) else p1.slvs_index
    arc["p2_i"] = p2 if isinstance(p2, int) else p2.slvs_index
    arc["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    arc["invert_direction"] = invert

    return self._init_entity(arc, fixed, construction, index_reference)

add_circle(nm, ct, radius, sketch, fixed=False, construction=False, index_reference=False)

Add a circle in 2d space.

Parameters:

Name Type Description Default
ct SlvsPoint2D

Circle's centerpoint.

required
radius float

Circle's radius.

required
sketch SlvsSketch

The sketch this circle belongs to.

required
nm SlvsNormal2D

Circle's normal.

required

Returns:

Name Type Description
SlvsCircle SlvsCircle

The created circle.

Source code in model/group_entities.py
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
def add_circle(
    self,
    nm: SlvsNormal2D,
    ct: SlvsPoint2D,
    radius: float,
    sketch: SlvsSketch,
    fixed: bool = False,
    construction: bool = False,
    index_reference: bool = False,
) -> SlvsCircle:
    """Add a circle in 2d space.

    Arguments:
        ct: Circle's centerpoint.
        radius: Circle's radius.
        sketch: The sketch this circle belongs to.
        nm: Circle's normal.

    Returns:
        SlvsCircle: The created circle.
    """
    c = self.circles.add()
    c["nm_i"] = nm if isinstance(nm, int) else nm.slvs_index
    c["ct_i"] = ct if isinstance(ct, int) else ct.slvs_index
    c["radius"] = float(radius)
    c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index

    return self._init_entity(c, fixed, construction, index_reference)

add_line_2d(p1, p2, sketch, fixed=False, construction=False, index_reference=False)

Add a line in 2d space.

Parameters:

Name Type Description Default
p1 SlvsPoint2D

Line's startpoint.

required
p2 SlvsPoint2D

Line's endpoint.

required
sketch SlvsSketch

The sketch this line belongs to.

required

Returns:

Name Type Description
SlvsLine2D SlvsLine2D

The created line.

Source code in model/group_entities.py
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
def add_line_2d(
    self,
    p1: SlvsPoint2D,
    p2: SlvsPoint2D,
    sketch: SlvsSketch,
    fixed: bool = False,
    construction: bool = False,
    index_reference: bool = False,
) -> SlvsLine2D:
    """Add a line in 2d space.

    Arguments:
        p1: Line's startpoint.
        p2: Line's endpoint.
        sketch: The sketch this line belongs to.

    Returns:
        SlvsLine2D: The created line.
    """
    line = self.lines2D.add()
    line["p1_i"] = p1 if isinstance(p1, int) else p1.slvs_index
    line["p2_i"] = p2 if isinstance(p2, int) else p2.slvs_index
    line["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index

    return self._init_entity(line, fixed, construction, index_reference)

add_line_3d(p1, p2, fixed=False, construction=False, index_reference=False)

Add a line in 3d space.

Parameters:

Name Type Description Default
p1 Union[SlvsPoint3D, int]

Line's startpoint.

required
p2 Union[SlvsPoint3D, int]

Line's endpoint.

required

Returns:

Name Type Description
SlvsLine3D SlvsLine3D

The created line.

Source code in model/group_entities.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
def add_line_3d(
    self,
    p1: Union[SlvsPoint3D, int],
    p2: Union[SlvsPoint3D, int],
    fixed: bool = False,
    construction: bool = False,
    index_reference: bool = False,
) -> SlvsLine3D:
    """Add a line in 3d space.

    Arguments:
        p1: Line's startpoint.
        p2: Line's endpoint.

    Returns:
        SlvsLine3D: The created line.
    """
    line = self.lines3D.add()
    line["p1_i"] = p1 if isinstance(p1, int) else p1.slvs_index
    line["p2_i"] = p2 if isinstance(p2, int) else p2.slvs_index

    return self._init_entity(line, fixed, construction, index_reference)

add_normal_2d(sketch, fixed=False, construction=False, index_reference=False)

Add a normal in 2d space.

Parameters:

Name Type Description Default
sketch SlvsSketch

The sketch this normal belongs to.

required

Returns:

Name Type Description
SlvsNormal2D SlvsNormal2D

The created normal.

Source code in model/group_entities.py
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
def add_normal_2d(
    self,
    sketch: SlvsSketch,
    fixed: bool = False,
    construction: bool = False,
    index_reference: bool = False,
) -> SlvsNormal2D:
    """Add a normal in 2d space.

    Arguments:
        sketch: The sketch this normal belongs to.

    Returns:
        SlvsNormal2D: The created normal.
    """
    nm = self.normals2D.add()
    nm["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index

    return self._init_entity(nm, fixed, construction, index_reference)

add_normal_3d(quat, fixed=False, construction=False, index_reference=False)

Add a normal in 3d space.

Parameters:

Name Type Description Default
quat Tuple[float, float, float, float]

Quaternion which describes the orientation.

required

Returns:

Name Type Description
SlvsNormal3D SlvsNormal3D

The created normal.

Source code in model/group_entities.py
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def add_normal_3d(
    self,
    quat: Tuple[float, float, float, float],
    fixed: bool = False,
    construction: bool = False,
    index_reference: bool = False,
) -> SlvsNormal3D:
    """Add a normal in 3d space.

    Arguments:
        quat: Quaternion which describes the orientation.

    Returns:
        SlvsNormal3D: The created normal.
    """
    nm = self.normals3D.add()
    nm["orientation"] = Quaternion(quat)

    return self._init_entity(nm, fixed, construction, index_reference)

add_point_2d(co, sketch, fixed=False, construction=False, index_reference=False)

Add a point in 2d space.

Parameters:

Name Type Description Default
co Tuple[float, float]

Coordinates of the point on the workplane.

required
sketch SlvsSketch

The sketch this point belongs to.

required

Returns:

Name Type Description
SlvsPoint2D SlvsPoint2D

The created point.

Source code in model/group_entities.py
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
def add_point_2d(
    self,
    co: Tuple[float, float],
    sketch: SlvsSketch,
    fixed: bool = False,
    construction: bool = False,
    index_reference: bool = False,
) -> SlvsPoint2D:
    """Add a point in 2d space.

    Arguments:
        co: Coordinates of the point on the workplane.
        sketch: The sketch this point belongs to.

    Returns:
        SlvsPoint2D: The created point.
    """
    p = self.points2D.add()
    p["co"] = Vector(co)
    p["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index

    return self._init_entity(p, fixed, construction, index_reference)

add_point_3d(co, fixed=False, construction=False, index_reference=False)

Add a point in 3d space.

Parameters:

Name Type Description Default
co Union[Tuple[float, float, float], Vector]

Location of the point in 3d space.

required

Returns:

Name Type Description
SlvsPoint3D Union[SlvsPoint3D, int]

The created point.

Source code in model/group_entities.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def add_point_3d(
    self,
    co: Union[Tuple[float, float, float], Vector],
    fixed: bool = False,
    construction: bool = False,
    index_reference: bool = False,
) -> Union[SlvsPoint3D, int]:
    """Add a point in 3d space.

    Arguments:
        co: Location of the point in 3d space.

    Returns:
        SlvsPoint3D: The created point.
    """
    if not hasattr(co, "__len__") or len(co) != 3:
        raise TypeError("Argument co must be of length 3")

    p = self.points3D.add()
    p["location"] = Vector(co)
    return self._init_entity(p, fixed, construction, index_reference)

add_sketch(wp, fixed=False, construction=False, index_reference=False)

Add a Sketch.

Parameters:

Name Type Description Default
wp SlvsWorkplane

Sketch's workplane.

required

Returns:

Name Type Description
SlvsSketch SlvsSketch

The created sketch.

Source code in model/group_entities.py
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
def add_sketch(
    self,
    wp: SlvsWorkplane,
    fixed: bool = False,
    construction: bool = False,
    index_reference: bool = False,
) -> SlvsSketch:
    """Add a Sketch.

    Arguments:
        wp: Sketch's workplane.

    Returns:
        SlvsSketch: The created sketch.
    """
    sketch = self.sketches.add()
    sketch["wp_i"] = wp if isinstance(wp, int) else wp.slvs_index

    retval = self._init_entity(sketch, fixed, construction, index_reference)
    index = retval if index_reference else retval.slvs_index
    _, i = self._breakdown_index(index)
    sketch.name = "Sketch"
    return retval

add_workplane(p1, nm, fixed=False, construction=False, index_reference=False)

Add a workplane.

Parameters:

Name Type Description Default
p1 SlvsPoint3D

Workplane's originpoint.

required
nm SlvsGenericEntity

Workplane's normal.

required

Returns:

Name Type Description
SlvsWorkplane SlvsWorkplane

The created workplane.

Source code in model/group_entities.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
def add_workplane(
    self,
    p1: SlvsPoint3D,
    nm: SlvsGenericEntity,
    fixed: bool = False,
    construction: bool = False,
    index_reference: bool = False,
) -> SlvsWorkplane:
    """Add a workplane.

    Arguments:
        p1: Workplane's originpoint.
        nm: Workplane's normal.

    Returns:
        SlvsWorkplane: The created workplane.
    """
    wp = self.workplanes.add()
    wp["p1_i"] = p1 if isinstance(p1, int) else p1.slvs_index
    wp["nm_i"] = nm if isinstance(nm, int) else nm.slvs_index

    return self._init_entity(wp, fixed, construction, index_reference)

get(index)

Get entity by index

Parameters:

Name Type Description Default
index int

The global index of the entity.

required

Returns:

Name Type Description
SlvsGenericEntity SlvsGenericEntity

Entity with the given global index or None if not found.

Source code in model/group_entities.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def get(self, index: int) -> SlvsGenericEntity:
    """Get entity by index

    Arguments:
        index: The global index of the entity.

    Returns:
        SlvsGenericEntity: Entity with the given global index or None if not found.
    """
    if index == -1:
        return None
    sub_list, i = self._get_list_and_index(index)
    if not sub_list or i >= len(sub_list):
        return None
    return sub_list[i]

remove(index)

Remove entity by index

Parameters:

Name Type Description Default
index int

The global index of the entity.

required
Source code in model/group_entities.py
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
def remove(self, index: int):
    """Remove entity by index

    Arguments:
        index: The global index of the entity.
    """
    assert isinstance(index, int)

    if self.get(index).origin:
        return

    entity_list, i = self._get_list_and_index(index)
    entity_list.remove(i)

    # Put last item to removed index and update all pointers to it
    last_index = len(entity_list) - 1

    if last_index < 0:
        return
    if i > last_index:
        return

    if not i == last_index:  # second last item was deleted
        entity_list.move(last_index, i)

    new_item = entity_list[i]
    update_pointers(bpy.context.scene, new_item.slvs_index, index)
    new_item.slvs_index = index

Bases: PropertyGroup

Source code in model/group_constraints.py
 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
class SlvsConstraints(PropertyGroup):

    _dimensional_constraints = (
        SlvsDistance,
        SlvsAngle,
        SlvsDiameter,
    )

    _geometric_constraints = (
        SlvsCoincident,
        SlvsEqual,
        SlvsParallel,
        SlvsHorizontal,
        SlvsVertical,
        SlvsTangent,
        SlvsMidpoint,
        SlvsPerpendicular,
        SlvsRatio,
    )

    _constraints = (
        SlvsCoincident,
        SlvsEqual,
        SlvsDistance,
        SlvsAngle,
        SlvsDiameter,
        SlvsParallel,
        SlvsHorizontal,
        SlvsVertical,
        SlvsTangent,
        SlvsMidpoint,
        SlvsPerpendicular,
        SlvsRatio,
    )

    __annotations__ = {
        cls.type.lower(): CollectionProperty(type=cls) for cls in _constraints
    }

    @classmethod
    def cls_from_type(cls, type: str):
        for constraint in cls._constraints:
            if type == constraint.type:
                return constraint
        return None

    def new_from_type(self, type: str) -> GenericConstraint:
        """Create a constraint by type.

        Arguments:
            type: Type of the constraint to be created.
        """
        name = type.lower()
        constraint_list = getattr(self, name)
        return constraint_list.add()

    def get_lists(self):
        lists = []
        for entity_list in self.rna_type.properties:
            name = entity_list.identifier
            if name in ("name", "rna_type"):
                continue
            lists.append(getattr(self, name))
        return lists

    def get_list(self, type: str):
        return getattr(self, type.lower())

    def get_from_type_index(self, type: str, index: int) -> GenericConstraint:
        """Get constraint by type and local index.

        Arguments:
            type: Constraint's type.
            index: Constraint's local index.

        Returns:
            GenericConstraint: Constraint with the given type and index or None if not found.
        """
        list = getattr(self, type.lower())
        if not list or index >= len(list):
            return None
        return list[index]

    def get_index(self, constr: GenericConstraint) -> int:
        """Get the index of a constraint in its collection.

        Arguments:
            constr: Constraint to get the index for.

        Returns:
            int: Index of the constraint or -1 if not found.
        """
        list = getattr(self, constr.type.lower())
        for i, item in enumerate(list):
            if item == constr:
                return i
        return -1

    def remove(self, constr: GenericConstraint):
        """Remove a constraint.

        Arguments:
            constr: Constraint to be removed.
        """
        i = self.get_index(constr)
        self.get_list(constr.type).remove(i)

    @property
    def dimensional(self):
        for constraint_type in self._dimensional_constraints:
            for entity in self.get_list(constraint_type.type):
                yield entity

    @property
    def geometric(self):
        for constraint_type in self._geometric_constraints:
            for entity in self.get_list(constraint_type.type):
                yield entity

    @property
    def all(self):
        for entity_list in self.get_lists():
            for entity in entity_list:
                yield entity

    def add_coincident(
        self,
        entity1: SlvsGenericEntity,
        entity2: SlvsGenericEntity,
        sketch: SlvsSketch = None,
    ) -> SlvsCoincident:
        """Add a coincident constraint.

        Arguments:
            entity1: -
            entity2: -
            sketch: The sketch this constraint belongs to.

        Returns:
            SlvsCoincident: The created constraint.
        """

        if all([e.is_point() for e in (entity1, entity2)]):
            # TODO: Implicitly merge points
            return

        c = self.coincident.add()
        c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
        c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
        if sketch:
            c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        return c

    def add_equal(
        self,
        entity1: SlvsGenericEntity,
        entity2: SlvsGenericEntity,
        sketch: Union[SlvsSketch, None] = None,
    ) -> SlvsEqual:
        """Add an equal constraint.

        Arguments:
            entity1: -
            entity2: -
            sketch: The sketch this constraint belongs to.

        Returns:
            SlvsEqual: The created constraint.
        """
        c = self.equal.add()
        c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
        c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
        if sketch is not None:
            c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        return c

    def add_distance(
        self,
        entity1: SlvsGenericEntity,
        entity2: Union[None, SlvsGenericEntity],
        sketch: Union[SlvsSketch, None] = None,
        init: bool = False,
        **settings,
    ) -> SlvsDistance:
        """Add a distance constraint.

        Arguments:
            entity1: -
            entity2: -
            sketch: The sketch this constraint belongs to.
            init: Initialize the constraint based on the given entities.

        Returns:
            SlvsDistance: The created constraint.
        """
        c = self.distance.add()
        c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index

        if entity2 is not None:
            c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
        if sketch is not None:
            c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        if init:
            c.assign_init_props(**settings)
        else:
            c.assign_settings(**settings)
        return c

    def add_angle(
        self,
        entity1: SlvsGenericEntity,
        entity2: SlvsGenericEntity,
        sketch: SlvsSketch = None,
        init: bool = False,
        **settings,
    ) -> SlvsAngle:
        """Add an angle constraint.

        Arguments:
            entity1: -
            entity2: -
            sketch: The sketch this constraint belongs to.
            init: Initialize the constraint based on the given entities.

        Returns:
            SlvsAngle: The created constraint.
        """
        c = self.angle.add()
        c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
        c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
        if sketch is not None:
            c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        if init:
            c.assign_init_props(**settings)
        else:
            c.assign_settings(**settings)
        return c

    def add_diameter(
        self,
        entity1: SlvsGenericEntity,
        sketch: SlvsSketch = None,
        init: bool = False,
        **settings,
    ) -> SlvsDiameter:
        """Add a diameter constraint.

        Arguments:
            entity1: -
            sketch: The sketch this constraint belongs to.
            init: Initialize the constraint based on the given entities.

        Returns:
            SlvsDiameter: The created constraint.
        """
        c = self.diameter.add()
        c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
        if sketch:
            c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        if init:
            c.assign_init_props(**settings)
        else:
            c.assign_settings(**settings)
        return c

    def add_parallel(
        self,
        entity1: SlvsGenericEntity,
        entity2: SlvsGenericEntity,
        sketch: Union[SlvsSketch, None] = None,
    ) -> SlvsParallel:
        """Add a parallel constraint.

        Arguments:
            entity1: -
            entity2: -
            sketch: The sketch this constraint belongs to.

        Returns:
            SlvsParallel: The created constraint.
        """
        c = self.parallel.add()
        c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
        c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
        if sketch is not None:
            c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        return c

    def add_horizontal(
        self,
        entity1: SlvsGenericEntity,
        entity2: SlvsGenericEntity = None,
        sketch: Union[SlvsSketch, None] = None,
    ) -> SlvsHorizontal:
        """Add a horizontal constraint.

        Arguments:
            entity1: -
            sketch: The sketch this constraint belongs to.

        Returns:
            SlvsHorizontal: The created constraint.
        """
        c = self.horizontal.add()
        c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
        if entity2 is not None:
            c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
        if sketch is not None:
            c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        return c

    def add_vertical(
        self,
        entity1: SlvsGenericEntity,
        entity2: SlvsGenericEntity = None,
        sketch: Union[SlvsSketch, None] = None,
    ) -> SlvsVertical:
        """Add a vertical constraint.

        Arguments:
            entity1: -
            sketch: The sketch this constraint belongs to.

        Returns:
            SlvsVertical: The created constraint.
        """
        c = self.vertical.add()
        c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
        if entity2 is not None:
            c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
        if sketch is not None:
            c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        return c

    def add_tangent(
        self,
        entity1: SlvsGenericEntity,
        entity2: SlvsGenericEntity,
        sketch: Union[SlvsSketch, None] = None,
    ) -> SlvsTangent:
        """Add a tangent constraint.

        Arguments:
            entity1: -
            entity2: -
            sketch: The sketch this constraint belongs to.

        Returns:
            SlvsTangent: The created constraint.
        """
        c = self.tangent.add()
        c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
        c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
        if sketch is not None:
            c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        return c

    def add_midpoint(
        self,
        entity1: SlvsGenericEntity,
        entity2: SlvsGenericEntity,
        sketch: Union[SlvsSketch, None] = None,
    ) -> SlvsMidpoint:
        """Add a midpoint constraint.

        Arguments:
            entity1: -
            entity2: -
            sketch: The sketch this constraint belongs to.

        Returns:
            SlvsMidpoint: The created constraint.
        """
        c = self.midpoint.add()
        c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
        c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
        if sketch is not None:
            c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        return c

    def add_perpendicular(
        self,
        entity1: SlvsGenericEntity,
        entity2: SlvsGenericEntity,
        sketch: Union[SlvsSketch, None] = None,
    ) -> SlvsPerpendicular:
        """Add a perpendicular constraint.

        Arguments:
            entity1: -
            entity2: -
            sketch: The sketch this constraint belongs to.

        Returns:
            SlvsPerpendicular: The created constraint.
        """
        c = self.perpendicular.add()
        c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
        c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
        if sketch is not None:
            c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        return c

    def add_ratio(
        self,
        entity1: SlvsGenericEntity,
        entity2: SlvsGenericEntity,
        sketch: Union[SlvsSketch, None] = None,
        init: bool = False,
        **settings,
    ) -> SlvsRatio:
        """Add a ratio constraint.

        Arguments:
            entity1: -
            entity2: -
            sketch: The sketch this constraint belongs to.
            init: Initialize the constraint based on the given entities.

        Returns:
            SlvsRatio: The created constraint.
        """
        c = self.ratio.add()
        c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
        c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
        if sketch is not None:
            c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
        if init:
            c.assign_init_props(**settings)
        else:
            c.assign_settings(**settings)
        return c

add_angle(entity1, entity2, sketch=None, init=False, **settings)

Add an angle constraint.

Parameters:

Name Type Description Default
entity1 SlvsGenericEntity

-

required
entity2 SlvsGenericEntity

-

required
sketch SlvsSketch

The sketch this constraint belongs to.

None
init bool

Initialize the constraint based on the given entities.

False

Returns:

Name Type Description
SlvsAngle SlvsAngle

The created constraint.

Source code in model/group_constraints.py
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
def add_angle(
    self,
    entity1: SlvsGenericEntity,
    entity2: SlvsGenericEntity,
    sketch: SlvsSketch = None,
    init: bool = False,
    **settings,
) -> SlvsAngle:
    """Add an angle constraint.

    Arguments:
        entity1: -
        entity2: -
        sketch: The sketch this constraint belongs to.
        init: Initialize the constraint based on the given entities.

    Returns:
        SlvsAngle: The created constraint.
    """
    c = self.angle.add()
    c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
    c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
    if sketch is not None:
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    if init:
        c.assign_init_props(**settings)
    else:
        c.assign_settings(**settings)
    return c

add_coincident(entity1, entity2, sketch=None)

Add a coincident constraint.

Parameters:

Name Type Description Default
entity1 SlvsGenericEntity

-

required
entity2 SlvsGenericEntity

-

required
sketch SlvsSketch

The sketch this constraint belongs to.

None

Returns:

Name Type Description
SlvsCoincident SlvsCoincident

The created constraint.

Source code in model/group_constraints.py
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
def add_coincident(
    self,
    entity1: SlvsGenericEntity,
    entity2: SlvsGenericEntity,
    sketch: SlvsSketch = None,
) -> SlvsCoincident:
    """Add a coincident constraint.

    Arguments:
        entity1: -
        entity2: -
        sketch: The sketch this constraint belongs to.

    Returns:
        SlvsCoincident: The created constraint.
    """

    if all([e.is_point() for e in (entity1, entity2)]):
        # TODO: Implicitly merge points
        return

    c = self.coincident.add()
    c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
    c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
    if sketch:
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    return c

add_diameter(entity1, sketch=None, init=False, **settings)

Add a diameter constraint.

Parameters:

Name Type Description Default
entity1 SlvsGenericEntity

-

required
sketch SlvsSketch

The sketch this constraint belongs to.

None
init bool

Initialize the constraint based on the given entities.

False

Returns:

Name Type Description
SlvsDiameter SlvsDiameter

The created constraint.

Source code in model/group_constraints.py
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
def add_diameter(
    self,
    entity1: SlvsGenericEntity,
    sketch: SlvsSketch = None,
    init: bool = False,
    **settings,
) -> SlvsDiameter:
    """Add a diameter constraint.

    Arguments:
        entity1: -
        sketch: The sketch this constraint belongs to.
        init: Initialize the constraint based on the given entities.

    Returns:
        SlvsDiameter: The created constraint.
    """
    c = self.diameter.add()
    c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
    if sketch:
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    if init:
        c.assign_init_props(**settings)
    else:
        c.assign_settings(**settings)
    return c

add_distance(entity1, entity2, sketch=None, init=False, **settings)

Add a distance constraint.

Parameters:

Name Type Description Default
entity1 SlvsGenericEntity

-

required
entity2 Union[None, SlvsGenericEntity]

-

required
sketch Union[SlvsSketch, None]

The sketch this constraint belongs to.

None
init bool

Initialize the constraint based on the given entities.

False

Returns:

Name Type Description
SlvsDistance SlvsDistance

The created constraint.

Source code in model/group_constraints.py
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
def add_distance(
    self,
    entity1: SlvsGenericEntity,
    entity2: Union[None, SlvsGenericEntity],
    sketch: Union[SlvsSketch, None] = None,
    init: bool = False,
    **settings,
) -> SlvsDistance:
    """Add a distance constraint.

    Arguments:
        entity1: -
        entity2: -
        sketch: The sketch this constraint belongs to.
        init: Initialize the constraint based on the given entities.

    Returns:
        SlvsDistance: The created constraint.
    """
    c = self.distance.add()
    c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index

    if entity2 is not None:
        c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
    if sketch is not None:
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    if init:
        c.assign_init_props(**settings)
    else:
        c.assign_settings(**settings)
    return c

add_equal(entity1, entity2, sketch=None)

Add an equal constraint.

Parameters:

Name Type Description Default
entity1 SlvsGenericEntity

-

required
entity2 SlvsGenericEntity

-

required
sketch Union[SlvsSketch, None]

The sketch this constraint belongs to.

None

Returns:

Name Type Description
SlvsEqual SlvsEqual

The created constraint.

Source code in model/group_constraints.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
def add_equal(
    self,
    entity1: SlvsGenericEntity,
    entity2: SlvsGenericEntity,
    sketch: Union[SlvsSketch, None] = None,
) -> SlvsEqual:
    """Add an equal constraint.

    Arguments:
        entity1: -
        entity2: -
        sketch: The sketch this constraint belongs to.

    Returns:
        SlvsEqual: The created constraint.
    """
    c = self.equal.add()
    c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
    c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
    if sketch is not None:
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    return c

add_horizontal(entity1, entity2=None, sketch=None)

Add a horizontal constraint.

Parameters:

Name Type Description Default
entity1 SlvsGenericEntity

-

required
sketch Union[SlvsSketch, None]

The sketch this constraint belongs to.

None

Returns:

Name Type Description
SlvsHorizontal SlvsHorizontal

The created constraint.

Source code in model/group_constraints.py
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
def add_horizontal(
    self,
    entity1: SlvsGenericEntity,
    entity2: SlvsGenericEntity = None,
    sketch: Union[SlvsSketch, None] = None,
) -> SlvsHorizontal:
    """Add a horizontal constraint.

    Arguments:
        entity1: -
        sketch: The sketch this constraint belongs to.

    Returns:
        SlvsHorizontal: The created constraint.
    """
    c = self.horizontal.add()
    c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
    if entity2 is not None:
        c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
    if sketch is not None:
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    return c

add_midpoint(entity1, entity2, sketch=None)

Add a midpoint constraint.

Parameters:

Name Type Description Default
entity1 SlvsGenericEntity

-

required
entity2 SlvsGenericEntity

-

required
sketch Union[SlvsSketch, None]

The sketch this constraint belongs to.

None

Returns:

Name Type Description
SlvsMidpoint SlvsMidpoint

The created constraint.

Source code in model/group_constraints.py
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
def add_midpoint(
    self,
    entity1: SlvsGenericEntity,
    entity2: SlvsGenericEntity,
    sketch: Union[SlvsSketch, None] = None,
) -> SlvsMidpoint:
    """Add a midpoint constraint.

    Arguments:
        entity1: -
        entity2: -
        sketch: The sketch this constraint belongs to.

    Returns:
        SlvsMidpoint: The created constraint.
    """
    c = self.midpoint.add()
    c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
    c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
    if sketch is not None:
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    return c

add_parallel(entity1, entity2, sketch=None)

Add a parallel constraint.

Parameters:

Name Type Description Default
entity1 SlvsGenericEntity

-

required
entity2 SlvsGenericEntity

-

required
sketch Union[SlvsSketch, None]

The sketch this constraint belongs to.

None

Returns:

Name Type Description
SlvsParallel SlvsParallel

The created constraint.

Source code in model/group_constraints.py
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
def add_parallel(
    self,
    entity1: SlvsGenericEntity,
    entity2: SlvsGenericEntity,
    sketch: Union[SlvsSketch, None] = None,
) -> SlvsParallel:
    """Add a parallel constraint.

    Arguments:
        entity1: -
        entity2: -
        sketch: The sketch this constraint belongs to.

    Returns:
        SlvsParallel: The created constraint.
    """
    c = self.parallel.add()
    c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
    c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
    if sketch is not None:
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    return c

add_perpendicular(entity1, entity2, sketch=None)

Add a perpendicular constraint.

Parameters:

Name Type Description Default
entity1 SlvsGenericEntity

-

required
entity2 SlvsGenericEntity

-

required
sketch Union[SlvsSketch, None]

The sketch this constraint belongs to.

None

Returns:

Name Type Description
SlvsPerpendicular SlvsPerpendicular

The created constraint.

Source code in model/group_constraints.py
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
def add_perpendicular(
    self,
    entity1: SlvsGenericEntity,
    entity2: SlvsGenericEntity,
    sketch: Union[SlvsSketch, None] = None,
) -> SlvsPerpendicular:
    """Add a perpendicular constraint.

    Arguments:
        entity1: -
        entity2: -
        sketch: The sketch this constraint belongs to.

    Returns:
        SlvsPerpendicular: The created constraint.
    """
    c = self.perpendicular.add()
    c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
    c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
    if sketch is not None:
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    return c

add_ratio(entity1, entity2, sketch=None, init=False, **settings)

Add a ratio constraint.

Parameters:

Name Type Description Default
entity1 SlvsGenericEntity

-

required
entity2 SlvsGenericEntity

-

required
sketch Union[SlvsSketch, None]

The sketch this constraint belongs to.

None
init bool

Initialize the constraint based on the given entities.

False

Returns:

Name Type Description
SlvsRatio SlvsRatio

The created constraint.

Source code in model/group_constraints.py
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
def add_ratio(
    self,
    entity1: SlvsGenericEntity,
    entity2: SlvsGenericEntity,
    sketch: Union[SlvsSketch, None] = None,
    init: bool = False,
    **settings,
) -> SlvsRatio:
    """Add a ratio constraint.

    Arguments:
        entity1: -
        entity2: -
        sketch: The sketch this constraint belongs to.
        init: Initialize the constraint based on the given entities.

    Returns:
        SlvsRatio: The created constraint.
    """
    c = self.ratio.add()
    c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
    c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
    if sketch is not None:
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    if init:
        c.assign_init_props(**settings)
    else:
        c.assign_settings(**settings)
    return c

add_tangent(entity1, entity2, sketch=None)

Add a tangent constraint.

Parameters:

Name Type Description Default
entity1 SlvsGenericEntity

-

required
entity2 SlvsGenericEntity

-

required
sketch Union[SlvsSketch, None]

The sketch this constraint belongs to.

None

Returns:

Name Type Description
SlvsTangent SlvsTangent

The created constraint.

Source code in model/group_constraints.py
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
def add_tangent(
    self,
    entity1: SlvsGenericEntity,
    entity2: SlvsGenericEntity,
    sketch: Union[SlvsSketch, None] = None,
) -> SlvsTangent:
    """Add a tangent constraint.

    Arguments:
        entity1: -
        entity2: -
        sketch: The sketch this constraint belongs to.

    Returns:
        SlvsTangent: The created constraint.
    """
    c = self.tangent.add()
    c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
    c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
    if sketch is not None:
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    return c

add_vertical(entity1, entity2=None, sketch=None)

Add a vertical constraint.

Parameters:

Name Type Description Default
entity1 SlvsGenericEntity

-

required
sketch Union[SlvsSketch, None]

The sketch this constraint belongs to.

None

Returns:

Name Type Description
SlvsVertical SlvsVertical

The created constraint.

Source code in model/group_constraints.py
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
def add_vertical(
    self,
    entity1: SlvsGenericEntity,
    entity2: SlvsGenericEntity = None,
    sketch: Union[SlvsSketch, None] = None,
) -> SlvsVertical:
    """Add a vertical constraint.

    Arguments:
        entity1: -
        sketch: The sketch this constraint belongs to.

    Returns:
        SlvsVertical: The created constraint.
    """
    c = self.vertical.add()
    c["entity1_i"] = entity1 if isinstance(entity1, int) else entity1.slvs_index
    if entity2 is not None:
        c["entity2_i"] = entity2 if isinstance(entity2, int) else entity2.slvs_index
    if sketch is not None:
        c["sketch_i"] = sketch if isinstance(sketch, int) else sketch.slvs_index
    return c

get_from_type_index(type, index)

Get constraint by type and local index.

Parameters:

Name Type Description Default
type str

Constraint's type.

required
index int

Constraint's local index.

required

Returns:

Name Type Description
GenericConstraint GenericConstraint

Constraint with the given type and index or None if not found.

Source code in model/group_constraints.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def get_from_type_index(self, type: str, index: int) -> GenericConstraint:
    """Get constraint by type and local index.

    Arguments:
        type: Constraint's type.
        index: Constraint's local index.

    Returns:
        GenericConstraint: Constraint with the given type and index or None if not found.
    """
    list = getattr(self, type.lower())
    if not list or index >= len(list):
        return None
    return list[index]

get_index(constr)

Get the index of a constraint in its collection.

Parameters:

Name Type Description Default
constr GenericConstraint

Constraint to get the index for.

required

Returns:

Name Type Description
int int

Index of the constraint or -1 if not found.

Source code in model/group_constraints.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def get_index(self, constr: GenericConstraint) -> int:
    """Get the index of a constraint in its collection.

    Arguments:
        constr: Constraint to get the index for.

    Returns:
        int: Index of the constraint or -1 if not found.
    """
    list = getattr(self, constr.type.lower())
    for i, item in enumerate(list):
        if item == constr:
            return i
    return -1

new_from_type(type)

Create a constraint by type.

Parameters:

Name Type Description Default
type str

Type of the constraint to be created.

required
Source code in model/group_constraints.py
74
75
76
77
78
79
80
81
82
def new_from_type(self, type: str) -> GenericConstraint:
    """Create a constraint by type.

    Arguments:
        type: Type of the constraint to be created.
    """
    name = type.lower()
    constraint_list = getattr(self, name)
    return constraint_list.add()

remove(constr)

Remove a constraint.

Parameters:

Name Type Description Default
constr GenericConstraint

Constraint to be removed.

required
Source code in model/group_constraints.py
126
127
128
129
130
131
132
133
def remove(self, constr: GenericConstraint):
    """Remove a constraint.

    Arguments:
        constr: Constraint to be removed.
    """
    i = self.get_index(constr)
    self.get_list(constr.type).remove(i)