Picking using GetEntity2 (Python)

Picking using GetEntity2 (Python)
 1# Example of picking using GetEntity2, showing how to take account of any
 2# transformations due to entities being in sub-blocks.
 3
 4# Ask the user to pick a line entity (recursing into sub-blocks)
 5pickInfo = impact.gui.picker.get_entity2( "Pick a line", True )
 6
 7if pickInfo.isNone():
 8    impact.gui.output_toolbox.add("You didn't pick anything")
 9else:
10    if pickInfo.entity.entity_type != ipEntityType.etLine:
11        impact.gui.output_toolbox.add("You didn't pick a line")
12    else:
13        ot = impact.gui.output_toolbox
14        ot.clear()
15        ent = pickInfo.entity
16        ot.add( "The picked line is in block '" + ent.block.full_name + "'" )
17        vecStart = None
18        try:
19            vecStart = impact.creator.vector( ent.start.X, ent.start.Y )
20        except Exception as exc:
21            pass
22        impact.gui.output_toolbox.add(f"Failed to create object via impact.creator.vector(): {exc}")
23        vecEnd = None
24        try:
25            vecEnd = impact.creator.vector( ent.end.X, ent.end.Y )
26        except Exception as exc:
27            pass
28        impact.gui.output_toolbox.add(f"Failed to create object via impact.creator.vector(): {exc}")
29
30        # If the project options are set to use world coordinates (ipCoordinateMode.cmWorld) then we don't need
31        # to do any transformations.
32        if impact.active_drawing.coordinate_system.mode == ipCoordinateMode.cmBlock:
33
34            ot.add( "In block coordinates:" )
35            ot.add( "Line start: " + vecStart.to_string() )
36            ot.add( "Line end: " + vecEnd.to_string() )
37
38            # The Start and End points will be given in that block's coordinate impact.system,
39            # so we need to use the transformation matrix to convert them into values
40            # appropriate to the layer (in other words, world coordinates).
41            mat = pickInfo.recursed_entity.matrix
42            vecStart.transform( mat )
43            vecEnd.transform( mat )
44
45        ot.add( "In layer coordinates:" )
46        ot.add( "Line start: " + vecStart.to_string() )
47        ot.add( "Line end: " + vecEnd.to_string() )