Beispiel #1
0
 def test_set_item_set_supported_dxf_attributes(self):
     query = EntityQuery([Line(), Circle(), Text()])
     query["layer"] = "MyLayer"
     assert all(e.dxf.layer == "MyLayer" for e in query) is True
Beispiel #2
0
def test_bool_select_3(modelspace):
    result = EntityQuery(modelspace,
                         '*[layer=="lay_lines" & (color==7 | color==6)]')
    # 1xLINE(layer=="lay_lines" & color==7) 1xPOLYLINE(layer=="lay_lines" & color==6)
    assert len(result) == 2
Beispiel #3
0
def test_ignore_case(modelspace):
    result = EntityQuery(modelspace, '*[layer=="LAY_lines"]i')
    # 1xLINE 1xPOLYLINE
    assert len(result) == 2
Beispiel #4
0
def test_match_regex_not_text(modelspace):
    result = EntityQuery(modelspace, '* !TEXT[layer ? "lay_.*"]')
    assert len(result) == 2
Beispiel #5
0
def test_not_supported_attribute(modelspace):
    result = EntityQuery(modelspace, '*[mozman!="TEST"]')
    assert len(result) == 0
Beispiel #6
0
    def query(self, query: str = '*') -> EntityQuery:
        """
        Get all DXF entities matching the :ref:`entity query string`.

        """
        return EntityQuery(iter(self), query)
Beispiel #7
0
def explode_block_reference(block_ref: 'Insert',
                            target_layout: 'BaseLayout') -> EntityQuery:
    """
    Explode a block reference into single DXF entities.

    Transforms the block entities into the required WCS location by applying the block reference
    attributes `insert`, `extrusion`, `rotation` and the scaling values `xscale`, `yscale` and `zscale`.
    Multiple inserts by row and column attributes is not supported.

    Returns an EntityQuery() container with all exploded DXF entities.

    Attached ATTRIB entities are converted to TEXT entities, this is the behavior of the BURST command of
    the AutoCAD Express Tools.

    Args:
        block_ref: Block reference entity (INSERT)
        target_layout: explicit target layout for exploded DXF entities

    .. warning::

        **Non uniform scaling** lead to incorrect results for text entities (TEXT, MTEXT, ATTRIB) and
        some other entities like ELLIPSE, SHAPE, HATCH with arc or ellipse path segments and
        POLYLINE/LWPOLYLINE with arc segments.

    (internal API)

    """
    if target_layout is None:
        raise DXFStructureError('Target layout is None.')

    if block_ref.doc is None:
        raise DXFStructureError(
            'Block reference has to be assigned to a DXF document.')

    entitydb = block_ref.doc.entitydb
    assert entitydb is not None, 'Exploding a block reference requires an entity database.'

    dxffactory = block_ref.doc.dxffactory
    assert dxffactory is not None, 'Exploding a block reference requires a DXF entity factory.'

    entities = []

    for entity in virtual_block_reference_entities(block_ref):
        dxftype = entity.dxftype()
        entitydb.add(entity)
        target_layout.add_entity(entity)
        if dxftype == 'DIMENSION':
            # Render a graphical representation for each exploded DIMENSION entity as anonymous block.
            cast('Dimension', entity).render()
        entities.append(entity)

    # Convert attached ATTRIB entities to TEXT entities:
    # This is the behavior of the BURST command of the AutoCAD Express Tools
    for attrib in block_ref.attribs:
        # Attached ATTRIB entities are already located in the WCS
        text = attrib_to_text(attrib, dxffactory)
        target_layout.add_entity(text)
        entities.append(text)

    source_layout = block_ref.get_layout()
    if source_layout is not None:
        # Remove and destroy exploded INSERT if assigned to a layout
        source_layout.delete_entity(block_ref)
    else:
        entitydb.delete_entity(block_ref)
    return EntityQuery(entities)
Beispiel #8
0
 def test_no_selected_dxf_attribute_raises_type_error(self):
     query = EntityQuery()
     with pytest.raises(TypeError):
         _ = query == "MyLayer"
Beispiel #9
0
 def test_greater_equal_raises_type_error_for_vector_attributes(self):
     query = EntityQuery([Text.new(dxfattribs={"insert": Vec3(0, 0)})])
     with pytest.raises(TypeError):
         _ = query["insert"] >= (1, 0)
Beispiel #10
0
 def entities(self):
     return EntityQuery([
         MText.new(
             dxfattribs={"text": "content"}),  # virtual text attribute
         Text.new(dxfattribs={"text": "content"})
     ])
Beispiel #11
0
def test_remove_supports_virtual_entities():
    result = EntityQuery([Text(), Line(), Arc()]).remove("TEXT")
    assert len(result) == 2
Beispiel #12
0
 def test_del_item_ignores_unsupported_attributes(self):
     query = EntityQuery([Line(), Text()])
     query["layer"] = "MyLayer"
     del query["layer"]
     assert query[0].dxf.layer == "0", "expected the default value"
     assert query[1].dxf.layer == "0", "expected the default value"
Beispiel #13
0
 def test_set_item_does_not_ignore_invalid_attribute_values(self):
     query = EntityQuery([Line(), Text()])
     with pytest.raises(TypeError):
         query["start"] = "InvalidVertex"
     with pytest.raises(ValueError):
         query["color"] = "red"
Beispiel #14
0
 def test_set_item_ignores_unsupported_attributes(self):
     query = EntityQuery([Line(), Text()])
     query["text"] = "MyText"
     assert query.query("TEXT").first.dxf.text == "MyText"
Beispiel #15
0
def test_select_line(modelspace):
    result = EntityQuery(modelspace, "LINE")
    assert len(result) == 1, "should be 1 LINE"
Beispiel #16
0
 def test_difference(self, base: EntityQuery):
     result = base - base.query("LINE")
     assert len(result) == 2
     assert set([e.dxftype() for e in result]) == {"CIRCLE", "TEXT"}
Beispiel #17
0
def test_select_layer_1(modelspace):
    result = EntityQuery(modelspace, '*[layer=="lay_lines"]')
    assert len(result) == 2
    assert result.first.dxftype() == "LINE"
    assert result.last.dxftype() == "POLYLINE"
Beispiel #18
0
 def test_intersection(self, base: EntityQuery):
     result = base & base.query("LINE")
     assert len(result) == 1
     assert [e.dxftype() for e in result] == ["LINE"]
Beispiel #19
0
def query_virtual_entities(entity: SupportsVirtualEntities) -> EntityQuery:
    return EntityQuery(virtual_entities(entity))
Beispiel #20
0
 def test_set_layer_attribute(self, entities: EntityQuery):
     entities.layer = "TEST"
     assert len(entities) == 2
     assert all(e.dxf.layer == "TEST" for e in entities) is True
Beispiel #21
0
def test_match_regex(modelspace):
    result = EntityQuery(modelspace, '*[layer ? "lay_.*"]')
    assert len(result) == 3
Beispiel #22
0
def test_empty_init():
    result = EntityQuery()
    assert len(result) == 0
Beispiel #23
0
def test_match_whole_string(modelspace):
    # re just compares the start of a string, check for an
    # implicit '$' at the end of the search string.
    result = EntityQuery(modelspace, '*[layer=="lay"]')
    assert len(result) == 0
Beispiel #24
0
def test_select_all(modelspace):
    result = EntityQuery(modelspace, "*")
    # 1xLINE, 1xPOLYLINE, 0xVERTEX, 0xSEQEND, 1x TEXT, 1x CIRCLE
    assert len(result.entities) == 4
    assert len(result) == 4
Beispiel #25
0
def test_bool_select(modelspace):
    result = EntityQuery(modelspace, '*[layer=="lay_lines" & color==7]')
    # 1xLINE
    assert len(result) == 1
Beispiel #26
0
def test_first(modelspace):
    result = EntityQuery(modelspace, "*")
    assert result.first.dxftype() == "LINE"
Beispiel #27
0
def test_bool_select_4(modelspace):
    result = EntityQuery(
        modelspace, '*[(layer=="lay_lines" | layer=="lay_text") & !color==7]')
    # 1xPOLYLINE(layer=="lay_lines" & color==6) 1xTEXT(layer=="lay_text" & color==6)
    assert len(result) == 2
Beispiel #28
0
def test_last(modelspace):
    result = EntityQuery(modelspace, "*")
    assert result.last.dxftype() == "CIRCLE"
Beispiel #29
0
def test_ignore_case_for_num_values(modelspace):
    result = EntityQuery(modelspace, '*[color==6]i')
    # 1xPOLYLINE 1xTEXT
    assert len(result) == 2
Beispiel #30
0
 def test_set_item_accepts_only_strings_as_key(self):
     query = EntityQuery()
     with pytest.raises(TypeError):
         query[1] = "has to fail"
     with pytest.raises(TypeError):
         query[:] = "has to fail"