LayerInsert example (Python)

LayerInsert example (Python)
 1# This example creates a new layer and some geometry in the new layer,
 2# an insert of the new layer is inserted into the first layer in the
 3# active drawing. Relative coordinates and vector parameters are used.
 4
 5# Create a local variable for the output toolbox
 6ot = impact.gui.output_toolbox
 7
 8# Clear the output toolbox
 9ot.clear()
10
11# Create a local variable for the active drawing
12ad = impact.active_drawing
13
14# check there is an active drawing
15if ad.isNone():
16
17    # display a message in the output toolbox
18    ot.add("Unable to continue: there is no active drawing")
19
20else:
21
22    # create a local variable for the current active layer
23    originalLayer = ad.active_layer
24
25    # create a new block from the selected entities in the active block
26    newLayer = None
27    try:
28        newLayer = ad.layers.add( "New Layer", "ONE_UP", originalLayer )
29    except Exception as exc:
30        pass
31    impact.gui.output_toolbox.add(f"Failed to create object via ad.layers.add(): {exc}")
32
33    # check the layer was created succesfully
34    if not newLayer.isNone():
35
36        # create a local variable for the active block
37        ab = ad.active_layer.active_block
38
39        # move to the origin of the active block
40        ab.move_ad(0, 0)
41
42        # draw some geometry
43        ab.lined(100, 0)
44        ab.lined(0, 100)
45        ab.lined(-100, 0)
46        ab.lined(0, -100)
47
48        # create some local variables for the offset, angle, scale etc. for the
49        # layer insert
50        offset = None
51        try:
52            offset = impact.creator.vector(500,500)
53        except Exception as exc:
54            pass
55        impact.gui.output_toolbox.add(f"Failed to create object via impact.creator.vector(): {exc}")
56        scale = 2
57        angle = 0
58        mirrorX = 0
59        mirrorY = 0
60
61        # set the active layer back to the original layer
62        ad.active_layer = originalLayer
63
64        # update the local variable for the active block
65        ab = ad.active_layer.active_block
66
67        # create the layer insert
68        layerInsert = ab.layer_insert( newLayer, offset, scale, angle, mirrorX, mirrorY )
69
70        # Display an appropriate message in the output toolbox
71        if not layerInsert.isNone():
72            ot.add("Successfully created a layer insert")
73        else:
74            ot.add("Failed to create a layer insert")
75
76    # View the extents of the active window
77    impact.gui.active_window.view_extents()