LayerInsertAd example (Python)

LayerInsertAd 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. Absolute coordinates and double 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 angle, scale etc. for the
49        # layer insert
50        scale = 2
51        angle = 0
52        mirrorX = 0
53        mirrorY = 0
54
55        # set the active layer back to the original layer
56        ad.active_layer = originalLayer
57
58        # update the local variable for the active block
59        ab = ad.active_layer.active_block
60
61        # create the layer insert
62        layerInsert = ab.layer_insert_ad( newLayer, 500, 500, scale, angle, mirrorX, mirrorY )
63
64        # Display an appropriate message in the output toolbox
65        if not layerInsert.isNone():
66            ot.add("Successfully created a layer insert")
67        else:
68            ot.add("Failed to create a layer insert")
69
70    # View the extents of the active window
71    impact.gui.active_window.view_extents()