BlockInsert example (Python)¶
BlockInsert example (Python)¶
1# this example creates 4 lines, creates a block from the lines
2# and creates another block insert of the new block using relative
3# coordinates and vector parameters
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 active block
23 ab = ad.active_layer.active_block
24
25 # ensure no existing entities are selected
26 ab.select_none()
27
28 # automatically select new entities
29 ab.auto_select = 1
30
31 # move to the origin of the active block
32 ab.move_ad(0, 0)
33
34 # draw some geometry
35 ab.lined(100, 0)
36 ab.lined(0, 100)
37 ab.lined(-100, 0)
38 ab.lined(0, -100)
39
40 # stop automatically selecting new entities
41 ab.auto_select = 0
42
43 # create a new block from the selected entities in the active block
44 blockInsert = None
45 try:
46 blockInsert = ab.block( "Block 1", "description", ipBlockPosition.bpLeft )
47 except Exception as exc:
48 pass
49 impact.gui.output_toolbox.add(f"Failed to create object via ab.block(): {exc}")
50
51 # check the block was created succesfully
52 if not blockInsert.isNone():
53
54 ot.add("Successfully created a new block")
55
56 # get the block that the block insert was an insert of and create
57 # another insert of that block
58 block = blockInsert.inserted_object
59
60 # create some local variables for the offset, angle, scale etc. for the
61 # block insert
62 offset = None
63 try:
64 offset = impact.creator.vector(500,500)
65 except Exception as exc:
66 pass
67 impact.gui.output_toolbox.add(f"Failed to create object via impact.creator.vector(): {exc}")
68 scale = 2
69 angle = 0
70 mirrorX = 0
71 mirrorY = 0
72
73 # create the block insert
74 newBlockInsert = ab.block_insert( block, offset, scale, angle, mirrorX, mirrorY )
75
76 # display an appropriate message in the output toolbox
77 if not newBlockInsert.isNone():
78 ot.add("Successfully created a new block insert")
79 else:
80 ot.add("An error occured creating the new block insert")
81
82 else:
83
84 # display an error message in the output toolbox
85 ot.add("An error occured creating the block")
86
87 # View the extents of the active window
88 impact.gui.active_window.view_extents()