How to use the group object tikzpy.grp
Examples of how to use the group object tikzpy.grp.
Example 1 - Add shapes and points to a group
Test code:
1#!/usr/bin/python
2
3### Load tikzpy library
4import tikzpy as py_tikZ
5import random, os, sys
6
7### Load main object
8tikZ = py_tikZ.load()
9
10### Create a cloud of points
11max_number = 10
12radius = 10
13ptos = []
14shps = []
15
16### Create a group
17grp = tikZ.grp.addgroup("grp1")
18
19for i in range(0,max_number):
20 ### Random points
21 x, y, z = random.uniform(-radius, radius), \
22 random.uniform(-radius, radius), \
23 random.uniform(-radius, radius)
24
25 ### Create a point
26 p = tikZ.pto.pto(x, y, z, layer=0, alias='pto%i' %i)
27 p1 = tikZ.pto.pto(x, y, z, layer=0, alias='ptoo%i' %i) #Not in shapes
28 if i > 0:
29 l = tikZ.shp.line(ptos[i-1], p)
30 l.arrow_build("","Latex",1)
31 shps.append(l)
32 grp.add = l
33
34 ### List of points
35 ptos.append(p.id)
36 tikZ.grp["grp1"] = p
37 tikZ.grp["grp1"] = p1
38
39print( "Points and shapes in first group")
40print( grp.ptos, grp.shps)
41
42### Create a second group
43grp1 = tikZ.grp.addgroup("grp2")
44grp1.add = ptos
45print( "Points and shapes in second group")
46print( grp1.ptos, grp1.shps)
47
48### Operate
49print( grp.ptos_of_shapes, grp.all_ptos)
50print( grp.ptos[-1].id)
51
52
53### Show points
54tikZ.pto.draw_points(ptos, color = "black")
55
56### Make drawing
57path = os.path.dirname(os.path.abspath(__file__))
58name = os.path.basename(os.path.abspath(__file__))
59name = os.path.splitext(name)[0]
60tikZ.save_pdf(path, name)
This would output:
Points and shapes in first group
[#0, #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16, #17, #18, #19] [#s0, #s1, #s2, #s3, #s4, #s5, #s6, #s7, #s8]
Points and shapes in second group
[#0, #2, #4, #6, #8, #10, #12, #14, #16, #18] []
[#0, #2, #4, #6, #8, #10, #12, #14, #16, #18] [#0, #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16, #17, #18, #19]
#19
tikzpy number points 20, number shapes 39
Example 2 - Add shapes and points to a group and copy, move it or translate
See the following example:
Drawing created with the following tikzpy code:
1#!/usr/bin/python
2
3### Load tikzpy library
4import tikzpy as py_tikZ
5import random, os, sys
6
7### Load main object
8tikZ = py_tikZ.load()
9
10### Create a cloud of points
11max_number = 10
12radius = 10
13
14### Create a group
15grp1 = tikZ.grp.addgroup("grp1")
16
17for i in range(0,max_number):
18 ### Random points
19 x, y, z = random.uniform(-radius, radius), \
20 random.uniform(-radius, radius), \
21 0 #random.uniform(-radius, radius)
22
23 ### Create a point
24 p = tikZ.pto.pto(x, y, z, layer=0, alias='pto%i' %i)
25
26 if i > 0:
27 l = tikZ.shp.line(grp1.ptos[-1], p)
28 l.arrow_build("","Latex",1)
29 grp1.add = l
30
31 ### List of points
32 grp1.add = p
33
34### Point copy
35grp2 = tikZ.grp.addgroup("grp2")
36grp2.add = tikZ.pto.copy(grp1.ptos, alias_prefix = "", alias_sufix = "")
37grp2.add = tikZ.shp.copy(grp1.shps)
38
39### Translate
40[[min_x,max_x],[min_y,max_y],[min_z,max_z]] = grp1.canvas(ptos = None)
41p1 = tikZ.pto.pto(min_x, (min_y + max_y)/2., (min_z + max_z)/2., layer=0)
42p2 = tikZ.pto.pto(max_x, (min_y + max_y)/2., (min_z + max_z)/2., layer=0)
43
44tikZ.pto.translate_to(grp2.ptos, p1, p2)
45tikZ.shp.translate_to(grp2.shps, p1, p2)
46tikZ.shp.translate(grp2.shps, x = 1., y = 0., z = 0.) #Offset of 1.
47for shp in grp2.shps:
48 shp.color = "orange"
49
50### Show points
51tikZ.pto.draw_points(ptos =grp1.ptos, color = "black")
52tikZ.pto.draw_points(ptos =grp2.ptos, color = "red")
53
54### Make drawing
55path = os.path.dirname(os.path.abspath(__file__))
56name = os.path.basename(os.path.abspath(__file__))
57name = os.path.splitext(name)[0]
58tikZ.save_pdf(path, name)
59
60
61
62
63
Example 3 - Add shapes and points to a group and copy, translate and rotate
See the following example:
Drawing created with the following tikzpy code:
1#!/usr/bin/python
2
3### Load tikzpy library
4import tikzpy as py_tikZ
5import os, sys
6
7### Load main object
8tikZ = py_tikZ.load()
9
10### Create a cloud of points
11max_number = 4
12radius = 5
13
14### Create a group
15angle = 0
16pm = tikZ.pto.pto(0, 0, 0)
17
18for i in range(0,max_number):
19
20 ### Create a point
21 p1 = tikZ.pto.pto(radius, 0, 0, layer=0, alias='pto1%i' %i)
22 p2 = tikZ.pto.pto(-radius, 0, 0, layer=0, alias='pto2%i' %i)
23 l = tikZ.shp.line(p1,p2)
24 c1 = tikZ.shp.circle(p1, radius*0.3, color = "green", fill="green!30")
25 c2 = tikZ.shp.circle(p2, radius*0.3, color = "red", fill="red!30")
26 c1.zorder = 1
27 c2.zorder = 1
28
29 grp = tikZ.grp.addgroup("grp%i" % i)
30 grp.add = [p1,p2,c1,c2, l]
31
32 if i > 0:
33 tikZ.shp.rotate(grp.shps, pm, Ax = 0, Ay = 0, Az = -180*i/max_number)
34
35print( tikZ.col.names)
36
37### Make drawing
38path = os.path.dirname(os.path.abspath(__file__))
39name = os.path.basename(os.path.abspath(__file__))
40name = os.path.splitext(name)[0]
41tikZ.save_pdf(path, name)