Update for changes in Blender's API
[blender-addons.git] / archimesh / achm_gltools.py
blobb2811e50d350bdc61bf814174f809b3f6828c25f
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 # <pep8 compliant>
21 # ----------------------------------------------------------
22 # support routines for OpenGL
23 # Author: Antonio Vazquez (antonioya)
25 # ----------------------------------------------------------
26 # noinspection PyUnresolvedReferences
27 import bpy
28 # noinspection PyUnresolvedReferences
29 import bgl
30 # noinspection PyUnresolvedReferences
31 import blf
32 from math import fabs, sqrt, sin, cos
33 # noinspection PyUnresolvedReferences
34 from mathutils import Vector
35 # noinspection PyUnresolvedReferences
36 from bpy_extras import view3d_utils
37 from .achm_room_maker import get_wall_points
40 # -------------------------------------------------------------
41 # Handle all draw routines (OpenGL main entry point)
43 # -------------------------------------------------------------
44 def draw_main(context):
45 region = context.region
46 rv3d = context.space_data.region_3d
47 scene = context.scene
49 rgb = scene.archimesh_text_color
50 rgbw = scene.archimesh_walltext_color
51 fsize = scene.archimesh_font_size
52 wfsize = scene.archimesh_wfont_size
53 space = scene.archimesh_hint_space
54 measure = scene.archimesh_gl_measure
55 dspname = scene.archimesh_gl_name
56 # Get visible layers
57 layers = []
58 for x in range(0, 20):
59 if context.scene.layers[x] is True:
60 layers.extend([x])
62 bgl.glEnable(bgl.GL_BLEND)
63 # Display selected or all
64 if scene.archimesh_gl_ghost is False:
65 objlist = context.selected_objects
66 else:
67 objlist = context.scene.objects
68 # ---------------------------------------
69 # Generate all OpenGL calls
70 # ---------------------------------------
71 for myobj in objlist:
72 if myobj.hide is False:
73 # verify visible layer
74 for x in range(0, 20):
75 if myobj.layers[x] is True:
76 if x in layers:
77 # -----------------------------------------------------
78 # Rooms
79 # -----------------------------------------------------
80 if 'RoomGenerator' in myobj:
81 op = myobj.RoomGenerator[0]
82 draw_room_data(myobj, op, region, rv3d, rgb, rgbw, fsize, wfsize, space, measure, dspname)
84 # -----------------------------------------------------
85 # Doors
86 # -----------------------------------------------------
87 if 'DoorObjectGenerator' in myobj:
88 op = myobj.DoorObjectGenerator[0]
89 draw_door_data(myobj, op, region, rv3d, rgb, fsize, space, measure)
91 # -----------------------------------------------------
92 # Window (Rail)
93 # -----------------------------------------------------
94 if 'WindowObjectGenerator' in myobj:
95 op = myobj.WindowObjectGenerator[0]
96 draw_window_rail_data(myobj, op, region, rv3d, rgb, fsize, space, measure)
98 # -----------------------------------------------------
99 # Window (Panel)
100 # -----------------------------------------------------
101 if 'WindowPanelGenerator' in myobj:
102 op = myobj.WindowPanelGenerator[0]
103 draw_window_panel_data(myobj, op, region, rv3d, rgb, fsize, space, measure)
104 break # avoid unnecessary loops
105 # -----------------------
106 # restore opengl defaults
107 # -----------------------
108 bgl.glLineWidth(1)
109 bgl.glDisable(bgl.GL_BLEND)
110 bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
113 # -------------------------------------------------------------
114 # Create OpenGL text
116 # right: Align to right
117 # -------------------------------------------------------------
118 def draw_text(x_pos, y_pos, display_text, rgb, fsize, right=False):
119 gap = 12
120 font_id = 0
121 blf.size(font_id, fsize, 72)
123 text_width, text_height = blf.dimensions(font_id, display_text)
124 if right is True:
125 newx = x_pos - text_width - gap
126 else:
127 newx = x_pos
128 blf.position(font_id, newx, y_pos, 0)
129 bgl.glColor4f(rgb[0], rgb[1], rgb[2], rgb[3])
130 blf.draw(font_id, display_text)
131 return
134 # -------------------------------------------------------------
135 # Draw an OpenGL line
137 # -------------------------------------------------------------
138 def draw_line(v1, v2):
139 # noinspection PyBroadException
140 try:
141 if v1 is not None and v2 is not None:
142 bgl.glBegin(bgl.GL_LINES)
143 bgl.glVertex2f(*v1)
144 bgl.glVertex2f(*v2)
145 bgl.glEnd()
146 except:
147 pass
150 # -------------------------------------------------------------
151 # Draw room information
153 # rgb: Color
154 # fsize: Font size
155 # -------------------------------------------------------------
156 def draw_room_data(myobj, op, region, rv3d, rgb, rgbw, fsize, wfsize, space, measure, dspname):
158 verts, activefaces, activenormals = get_wall_points(myobj)
160 # --------------------------
161 # Get line points and draw
162 # --------------------------
163 for face in activefaces:
164 a1 = None
165 b1 = None
166 a2 = None
167 b2 = None
168 # Bottom
169 for e in face:
170 if verts[e][2] == 0:
171 if a1 is None:
172 a1 = e
173 else:
174 b1 = e
175 # Top
176 for e in face:
177 if verts[e][2] != 0:
178 if round(verts[a1][0], 5) == round(verts[e][0], 5) and round(verts[a1][1], 5) == round(verts[e][1], 5):
179 a2 = e
180 else:
181 b2 = e
182 # Points
183 # a1_p = get_point((verts[a1][0], verts[a1][1], verts[a1][2]), myobj) # bottom
184 a2_p = get_point((verts[a2][0], verts[a2][1], verts[a2][2] + space), myobj) # top
185 a2_s1 = get_point((verts[a2][0], verts[a2][1], verts[a2][2]), myobj) # vertical line
186 a2_s2 = get_point((verts[a2][0], verts[a2][1], verts[a2][2] + space + fsize / 200), myobj) # vertical line
188 # b1_p = get_point((verts[b1][0], verts[b1][1], verts[b1][2]), myobj) # bottom
189 b2_p = get_point((verts[b2][0], verts[b2][1], verts[b2][2] + space), myobj) # top
190 b2_s1 = get_point((verts[b2][0], verts[b2][1], verts[b2][2]), myobj) # vertical line
191 b2_s2 = get_point((verts[b2][0], verts[b2][1], verts[b2][2] + space + fsize / 200), myobj) # vertical line
193 # converting to screen coordinates
194 screen_point_a = view3d_utils.location_3d_to_region_2d(region, rv3d, a2_p)
195 screen_point_b = view3d_utils.location_3d_to_region_2d(region, rv3d, b2_p)
196 screen_point_a1 = view3d_utils.location_3d_to_region_2d(region, rv3d, a2_s1)
197 screen_point_b1 = view3d_utils.location_3d_to_region_2d(region, rv3d, b2_s1)
198 screen_point_a2 = view3d_utils.location_3d_to_region_2d(region, rv3d, a2_s2)
199 screen_point_b2 = view3d_utils.location_3d_to_region_2d(region, rv3d, b2_s2)
201 # colour + line setup
202 bgl.glEnable(bgl.GL_BLEND)
203 bgl.glLineWidth(1)
204 bgl.glColor4f(rgb[0], rgb[1], rgb[2], rgb[3])
206 # --------------------------------
207 # Measures
208 # --------------------------------
209 if measure is True:
210 # Draw text
211 dist = distance(a2_p, b2_p)
212 txtpoint3d = interpolate3d(a2_p, b2_p, fabs(dist / 2))
213 # add a gap
214 gap3d = (txtpoint3d[0], txtpoint3d[1], txtpoint3d[2] + 0.05)
216 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
218 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize)
220 # Draw horizontal line
221 draw_line(screen_point_a, screen_point_b)
222 # Draw vertical line 1 (upper vertical)
223 draw_line(screen_point_a1, screen_point_a2)
224 # Draw vertical line 2 (upper vertical)
225 draw_line(screen_point_b1, screen_point_b2)
226 # Draw vertical line 1
227 draw_line(screen_point_a, screen_point_a1)
228 # Draw vertical line 2
229 draw_line(screen_point_b, screen_point_b1)
231 # --------------------------------
232 # Wall Number
233 # --------------------------------
234 if dspname is True:
235 for i in range(0, op.wall_num):
236 ap = get_point((op.walls[i].glpoint_a[0], op.walls[i].glpoint_a[1], op.walls[i].glpoint_a[2]), myobj)
237 bp = get_point((op.walls[i].glpoint_b[0], op.walls[i].glpoint_b[1], op.walls[i].glpoint_b[2]), myobj)
239 dist = distance(ap, bp)
240 txtpoint3d = interpolate3d(ap, bp, fabs(dist / 2))
241 # add a gap
242 gap3d = (txtpoint3d[0], txtpoint3d[1], txtpoint3d[2]) # + op.room_height / 2)
243 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
244 txt = "Wall: "
245 if op.walls[i].a is True:
246 txt = "Advance: "
247 if op.walls[i].curved is True:
248 txt = "Curved: "
250 draw_text(txtpoint2d[0], txtpoint2d[1], txt + str(i + 1), rgbw, wfsize)
252 return
255 # -------------------------------------------------------------
256 # Draw door information
258 # rgb: Color
259 # fsize: Font size
260 # -------------------------------------------------------------
261 def draw_door_data(myobj, op, region, rv3d, rgb, fsize, space, measure):
263 # Points
264 a_p1 = get_point(op.glpoint_a, myobj)
265 a_p2 = get_point((op.glpoint_a[0] - space, op.glpoint_a[1], op.glpoint_a[2]), myobj)
266 a_p3 = get_point((op.glpoint_a[0] - space - fsize / 200, op.glpoint_a[1], op.glpoint_a[2]), myobj)
268 t_p1 = get_point(op.glpoint_b, myobj)
269 t_p2 = get_point((op.glpoint_b[0] - space, op.glpoint_b[1], op.glpoint_b[2]), myobj)
270 t_p3 = get_point((op.glpoint_b[0] - space - fsize / 200, op.glpoint_b[1], op.glpoint_b[2]), myobj)
272 b_p1 = get_point(op.glpoint_b, myobj)
273 b_p2 = get_point((op.glpoint_b[0], op.glpoint_b[1], op.glpoint_b[2] + space), myobj)
274 b_p3 = get_point((op.glpoint_b[0], op.glpoint_b[1], op.glpoint_b[2] + space + fsize / 200), myobj)
276 c_p1 = get_point(op.glpoint_c, myobj)
277 c_p2 = get_point((op.glpoint_c[0], op.glpoint_c[1], op.glpoint_c[2] + space), myobj)
278 c_p3 = get_point((op.glpoint_c[0], op.glpoint_c[1], op.glpoint_c[2] + space + fsize / 200), myobj)
280 d_p1 = get_point(op.glpoint_d, myobj)
281 d_p2 = get_point((op.glpoint_d[0], op.glpoint_d[1], op.glpoint_b[2] + space + fsize / 300), myobj)
282 d_p3 = get_point((op.glpoint_d[0], op.glpoint_d[1], op.glpoint_d[2] - fsize / 250), myobj)
284 e_p1 = get_point(op.glpoint_e, myobj)
285 e_p2 = get_point((op.glpoint_e[0], op.glpoint_e[1], op.glpoint_b[2] + space + fsize / 300), myobj)
286 e_p3 = get_point((op.glpoint_e[0], op.glpoint_e[1], op.glpoint_e[2] - fsize / 250), myobj)
288 # converting to screen coordinates
289 screen_point_ap1 = view3d_utils.location_3d_to_region_2d(region, rv3d, a_p1)
290 screen_point_bp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, b_p1)
291 screen_point_cp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, c_p1)
292 screen_point_tp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, t_p1)
294 screen_point_ap2 = view3d_utils.location_3d_to_region_2d(region, rv3d, a_p2)
295 screen_point_bp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, b_p2)
296 screen_point_cp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, c_p2)
297 screen_point_tp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, t_p2)
299 screen_point_ap3 = view3d_utils.location_3d_to_region_2d(region, rv3d, a_p3)
300 screen_point_bp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, b_p3)
301 screen_point_cp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, c_p3)
302 screen_point_tp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, t_p3)
304 screen_point_dp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, d_p1)
305 screen_point_dp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, d_p2)
306 screen_point_dp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, d_p3)
308 screen_point_ep1 = view3d_utils.location_3d_to_region_2d(region, rv3d, e_p1)
309 screen_point_ep2 = view3d_utils.location_3d_to_region_2d(region, rv3d, e_p2)
310 screen_point_ep3 = view3d_utils.location_3d_to_region_2d(region, rv3d, e_p3)
312 # colour + line setup
313 bgl.glEnable(bgl.GL_BLEND)
314 bgl.glLineWidth(1)
315 bgl.glColor4f(rgb[0], rgb[1], rgb[2], rgb[3])
317 # --------------------------------
318 # Measures
319 # --------------------------------
320 if measure is True:
321 # Vertical
322 dist = distance(a_p1, t_p1)
323 txtpoint3d = interpolate3d(a_p1, t_p1, fabs(dist / 2))
324 gap3d = (a_p2[0], txtpoint3d[1], txtpoint3d[2])
325 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
326 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize, True)
328 draw_line(screen_point_ap2, screen_point_tp2)
329 draw_line(screen_point_ap3, screen_point_ap1)
330 draw_line(screen_point_tp3, screen_point_tp1)
332 # Horizontal
333 dist = distance(b_p1, c_p1)
334 txtpoint3d = interpolate3d(b_p1, c_p1, fabs(dist / 2))
335 gap3d = (txtpoint3d[0], txtpoint3d[1], b_p2[2] + 0.02)
336 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
337 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize)
339 draw_line(screen_point_bp2, screen_point_cp2)
340 draw_line(screen_point_bp3, screen_point_bp1)
341 draw_line(screen_point_cp3, screen_point_cp1)
343 # Door size
344 dist = distance(d_p1, e_p1)
345 txtpoint3d = interpolate3d(d_p1, e_p1, fabs(dist / 2))
346 gap3d = (txtpoint3d[0], txtpoint3d[1], txtpoint3d[2] + 0.02)
347 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
348 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize)
350 draw_line(screen_point_dp1, screen_point_ep1)
351 draw_line(screen_point_dp2, screen_point_dp3)
352 draw_line(screen_point_ep2, screen_point_ep3)
353 return
356 # -------------------------------------------------------------
357 # Draw window rail information
359 # rgb: Color
360 # fsize: Font size
361 # -------------------------------------------------------------
362 def draw_window_rail_data(myobj, op, region, rv3d, rgb, fsize, space, measure):
364 # Points
365 a_p1 = get_point(op.glpoint_a, myobj)
366 a_p2 = get_point((op.glpoint_a[0] - space, op.glpoint_a[1], op.glpoint_a[2]), myobj)
367 a_p3 = get_point((op.glpoint_a[0] - space - fsize / 200, op.glpoint_a[1], op.glpoint_a[2]), myobj)
369 t_p1 = get_point(op.glpoint_b, myobj)
370 t_p2 = get_point((op.glpoint_b[0] - space, op.glpoint_b[1], op.glpoint_b[2]), myobj)
371 t_p3 = get_point((op.glpoint_b[0] - space - fsize / 200, op.glpoint_b[1], op.glpoint_b[2]), myobj)
373 b_p1 = get_point(op.glpoint_b, myobj)
374 b_p2 = get_point((op.glpoint_b[0], op.glpoint_b[1], op.glpoint_b[2] + space), myobj)
375 b_p3 = get_point((op.glpoint_b[0], op.glpoint_b[1], op.glpoint_b[2] + space + fsize / 200), myobj)
377 c_p1 = get_point(op.glpoint_c, myobj)
378 c_p2 = get_point((op.glpoint_c[0], op.glpoint_c[1], op.glpoint_c[2] + space), myobj)
379 c_p3 = get_point((op.glpoint_c[0], op.glpoint_c[1], op.glpoint_c[2] + space + fsize / 200), myobj)
381 # converting to screen coordinates
382 screen_point_ap1 = view3d_utils.location_3d_to_region_2d(region, rv3d, a_p1)
383 screen_point_bp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, b_p1)
384 screen_point_cp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, c_p1)
385 screen_point_tp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, t_p1)
387 screen_point_ap2 = view3d_utils.location_3d_to_region_2d(region, rv3d, a_p2)
388 screen_point_bp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, b_p2)
389 screen_point_cp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, c_p2)
390 screen_point_tp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, t_p2)
392 screen_point_ap3 = view3d_utils.location_3d_to_region_2d(region, rv3d, a_p3)
393 screen_point_bp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, b_p3)
394 screen_point_cp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, c_p3)
395 screen_point_tp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, t_p3)
397 # colour + line setup
398 bgl.glEnable(bgl.GL_BLEND)
399 bgl.glLineWidth(1)
400 bgl.glColor4f(rgb[0], rgb[1], rgb[2], rgb[3])
402 # --------------------------------
403 # Measures
404 # --------------------------------
405 if measure is True:
406 # Vertical
407 dist = distance(a_p1, t_p1)
408 txtpoint3d = interpolate3d(a_p1, t_p1, fabs(dist / 2))
409 gap3d = (a_p2[0], txtpoint3d[1], txtpoint3d[2])
410 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
411 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize, True)
413 draw_line(screen_point_ap2, screen_point_tp2)
414 draw_line(screen_point_ap3, screen_point_ap1)
415 draw_line(screen_point_tp3, screen_point_tp1)
417 # Horizontal
418 dist = distance(b_p1, c_p1)
419 txtpoint3d = interpolate3d(b_p1, c_p1, fabs(dist / 2))
420 gap3d = (txtpoint3d[0], txtpoint3d[1], b_p2[2] + 0.02)
421 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
422 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize)
424 draw_line(screen_point_bp2, screen_point_cp2)
425 draw_line(screen_point_bp3, screen_point_bp1)
426 draw_line(screen_point_cp3, screen_point_cp1)
428 return
431 # -------------------------------------------------------------
432 # Draw window panel information
434 # rgb: Color
435 # fsize: Font size
436 # -------------------------------------------------------------
437 def draw_window_panel_data(myobj, op, region, rv3d, rgb, fsize, space, measure):
439 # Points
440 a_p1 = get_point(op.glpoint_a, myobj)
441 a_p2 = get_point((op.glpoint_a[0] - space, op.glpoint_a[1], op.glpoint_a[2]), myobj)
442 a_p3 = get_point((op.glpoint_a[0] - space - fsize / 200, op.glpoint_a[1], op.glpoint_a[2]), myobj)
444 f_p1 = get_point((op.glpoint_c[0], op.glpoint_c[1], op.glpoint_a[2]), myobj)
445 f_p2 = get_point((op.glpoint_c[0] + space, op.glpoint_c[1], op.glpoint_a[2]), myobj)
446 f_p3 = get_point((op.glpoint_c[0] + space + fsize / 200, op.glpoint_c[1], op.glpoint_a[2]), myobj)
448 t_p1 = get_point(op.glpoint_b, myobj)
449 t_p2 = get_point((op.glpoint_b[0] - space, op.glpoint_b[1], op.glpoint_b[2]), myobj)
450 t_p3 = get_point((op.glpoint_b[0] - space - fsize / 200, op.glpoint_b[1], op.glpoint_b[2]), myobj)
452 b_p1 = get_point(op.glpoint_b, myobj)
453 b_p2 = get_point((op.glpoint_b[0], op.glpoint_b[1], op.glpoint_b[2] + space), myobj)
454 b_p3 = get_point((op.glpoint_b[0], op.glpoint_b[1], op.glpoint_b[2] + space + fsize / 200), myobj)
456 c_p1 = get_point(op.glpoint_c, myobj)
457 c_p2 = get_point((op.glpoint_c[0], op.glpoint_c[1], op.glpoint_c[2] + space), myobj)
458 c_p3 = get_point((op.glpoint_c[0], op.glpoint_c[1], op.glpoint_c[2] + space + fsize / 200), myobj)
460 d_p1 = get_point(op.glpoint_c, myobj)
461 d_p2 = get_point((op.glpoint_c[0] + space, op.glpoint_c[1], op.glpoint_c[2]), myobj)
462 d_p3 = get_point((op.glpoint_c[0] + space + fsize / 200, op.glpoint_c[1], op.glpoint_c[2]), myobj)
464 g_p2 = get_point((op.glpoint_d[0], op.glpoint_d[1], 0), myobj)
465 g_p3 = get_point((op.glpoint_d[0], op.glpoint_d[1], op.glpoint_d[2]), myobj)
466 g_p4 = get_point((op.glpoint_d[0], op.glpoint_d[1], op.glpoint_d[2] + space), myobj)
467 g_p5 = get_point((op.glpoint_d[0], op.glpoint_d[1], op.glpoint_d[2] + space + fsize / 200), myobj)
469 h_p1 = get_point((op.glpoint_a[0], op.glpoint_a[1], op.glpoint_a[2] - space), myobj)
470 h_p2 = get_point((op.glpoint_a[0], op.glpoint_a[1], op.glpoint_a[2] - space - fsize / 200), myobj)
472 h_p3 = get_point((op.glpoint_c[0], op.glpoint_a[1], op.glpoint_a[2]), myobj)
473 h_p4 = get_point((op.glpoint_c[0], op.glpoint_a[1], op.glpoint_a[2] - space), myobj)
474 h_p5 = get_point((op.glpoint_c[0], op.glpoint_a[1], op.glpoint_a[2] - space - fsize / 200), myobj)
476 # converting to screen coordinates
477 screen_point_ap1 = view3d_utils.location_3d_to_region_2d(region, rv3d, a_p1)
478 screen_point_bp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, b_p1)
479 screen_point_cp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, c_p1)
480 screen_point_tp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, t_p1)
482 screen_point_ap2 = view3d_utils.location_3d_to_region_2d(region, rv3d, a_p2)
483 screen_point_bp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, b_p2)
484 screen_point_cp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, c_p2)
485 screen_point_tp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, t_p2)
487 screen_point_ap3 = view3d_utils.location_3d_to_region_2d(region, rv3d, a_p3)
488 screen_point_bp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, b_p3)
489 screen_point_cp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, c_p3)
490 screen_point_tp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, t_p3)
492 screen_point_dp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, d_p1)
493 screen_point_dp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, d_p2)
494 screen_point_dp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, d_p3)
496 screen_point_fp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, f_p1)
497 screen_point_fp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, f_p2)
498 screen_point_fp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, f_p3)
500 screen_point_gp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, g_p2)
501 screen_point_gp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, g_p3)
502 screen_point_gp4 = view3d_utils.location_3d_to_region_2d(region, rv3d, g_p4)
503 screen_point_gp5 = view3d_utils.location_3d_to_region_2d(region, rv3d, g_p5)
504 # colour + line setup
505 bgl.glEnable(bgl.GL_BLEND)
506 bgl.glLineWidth(1)
507 bgl.glColor4f(rgb[0], rgb[1], rgb[2], rgb[3])
509 # --------------------------------
510 # Measures
511 # --------------------------------
512 if measure is True:
513 # Vertical (right)
514 dist = distance(a_p1, t_p1)
515 txtpoint3d = interpolate3d(a_p1, t_p1, fabs(dist / 2))
516 gap3d = (a_p2[0], txtpoint3d[1], txtpoint3d[2])
517 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
518 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize, True)
520 draw_line(screen_point_ap2, screen_point_tp2)
521 draw_line(screen_point_ap3, screen_point_ap1)
522 draw_line(screen_point_tp3, screen_point_tp1)
524 # Vertical (Left)
525 dist = distance(f_p1, d_p1)
526 txtpoint3d = interpolate3d(f_p1, d_p1, fabs(dist / 2))
527 gap3d = (f_p2[0], txtpoint3d[1], txtpoint3d[2])
528 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
529 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize)
531 draw_line(screen_point_fp2, screen_point_dp2)
532 draw_line(screen_point_fp1, screen_point_fp3)
533 draw_line(screen_point_dp1, screen_point_dp3)
535 # Horizontal (not triangle nor arch)
536 if op.UST != "4" and op.UST != "2":
537 dist = distance(b_p1, c_p1)
538 txtpoint3d = interpolate3d(b_p2, c_p2, fabs(dist / 2))
539 gap3d = (txtpoint3d[0], txtpoint3d[1], txtpoint3d[2] + 0.05)
540 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
541 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize)
543 draw_line(screen_point_bp2, screen_point_cp2)
544 draw_line(screen_point_bp3, screen_point_bp1)
545 draw_line(screen_point_cp3, screen_point_cp1)
546 else:
547 dist = distance(b_p1, g_p3)
548 txtpoint3d = interpolate3d(b_p2, g_p4, fabs(dist / 2))
549 gap3d = (txtpoint3d[0], txtpoint3d[1], txtpoint3d[2] + 0.05)
550 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
551 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize, True)
553 dist = distance(g_p3, c_p1)
554 txtpoint3d = interpolate3d(g_p4, c_p2, fabs(dist / 2))
555 gap3d = (txtpoint3d[0], txtpoint3d[1], txtpoint3d[2] + 0.05)
556 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
557 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize)
559 draw_line(screen_point_bp2, screen_point_gp4)
560 draw_line(screen_point_gp4, screen_point_cp2)
561 draw_line(screen_point_bp3, screen_point_bp1)
562 draw_line(screen_point_cp3, screen_point_cp1)
563 draw_line(screen_point_gp3, screen_point_gp5)
565 # Only for Triangle or arch
566 if op.UST == "2" or op.UST == "4":
567 dist = distance(g_p2, g_p3)
568 txtpoint3d = interpolate3d(g_p2, g_p3, fabs(dist / 2))
569 gap3d = (txtpoint3d[0] + 0.05, txtpoint3d[1], txtpoint3d[2])
570 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
571 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize)
573 draw_line(screen_point_gp2, screen_point_gp3)
575 # Only for Triangle and Inclines or arch
576 if op.UST == "3" or op.UST == "4" or op.UST == "2":
577 screen_point_hp1 = view3d_utils.location_3d_to_region_2d(region, rv3d, h_p1)
578 screen_point_hp2 = view3d_utils.location_3d_to_region_2d(region, rv3d, h_p2)
579 screen_point_hp3 = view3d_utils.location_3d_to_region_2d(region, rv3d, h_p3)
580 screen_point_hp4 = view3d_utils.location_3d_to_region_2d(region, rv3d, h_p4)
581 screen_point_hp5 = view3d_utils.location_3d_to_region_2d(region, rv3d, h_p5)
583 dist = distance(h_p1, h_p3)
584 txtpoint3d = interpolate3d(h_p1, h_p3, fabs(dist / 2))
585 gap3d = (txtpoint3d[0], txtpoint3d[1], txtpoint3d[2] - space - 0.05)
586 txtpoint2d = view3d_utils.location_3d_to_region_2d(region, rv3d, gap3d)
587 draw_text(txtpoint2d[0], txtpoint2d[1], "%6.2f" % dist, rgb, fsize)
589 draw_line(screen_point_ap1, screen_point_hp2)
590 draw_line(screen_point_hp3, screen_point_hp5)
591 draw_line(screen_point_hp1, screen_point_hp4)
593 return
596 # --------------------------------------------------------------------
597 # Distance between 2 points in 3D space
598 # v1: first point
599 # v2: second point
600 # return: distance
601 # --------------------------------------------------------------------
602 def distance(v1, v2):
603 return sqrt((v2[0] - v1[0]) ** 2 + (v2[1] - v1[1]) ** 2 + (v2[2] - v1[2]) ** 2)
606 # --------------------------------------------------------------------
607 # Interpolate 2 points in 3D space
608 # v1: first point
609 # v2: second point
610 # d1: distance
611 # return: interpolate point
612 # --------------------------------------------------------------------
613 def interpolate3d(v1, v2, d1):
614 # calculate vector
615 v = (v2[0] - v1[0], v2[1] - v1[1], v2[2] - v1[2])
616 # calculate distance between points
617 d0 = distance(v1, v2)
618 # calculate interpolate factor (distance from origin / distance total)
619 # if d1 > d0, the point is projected in 3D space
620 if d0 > 0:
621 x = d1 / d0
622 else:
623 x = d1
625 final = (v1[0] + (v[0] * x), v1[1] + (v[1] * x), v1[2] + (v[2] * x))
626 return final
629 # --------------------------------------------------------------------
630 # Get point rotated and relative to parent
631 # v1: point
632 # mainobject
633 # --------------------------------------------------------------------
634 def get_point(v1, mainobject):
636 # Using World Matrix
637 vt = Vector((v1[0], v1[1], v1[2], 1))
638 m4 = mainobject.matrix_world
639 vt2 = m4 * vt
640 v2 = [vt2[0], vt2[1], vt2[2]]
642 return v2
645 # --------------------------------------------------------------------
646 # rotate point EULER X
647 # v1: point
648 # rad: Angles of rotation in Radians
649 # --------------------------------------------------------------------
650 def rotate_x(v1, rot):
651 v2 = [0, 0, 0]
653 radx = rot[0]
655 # X axis
656 v2[0] = v1[0]
657 v2[1] = v1[1] * cos(radx) - v1[2] * sin(radx)
658 v2[2] = v1[1] * sin(radx) + v1[2] * cos(radx)
660 return v2
663 # --------------------------------------------------------------------
664 # rotate point EULER Y
665 # v1: point
666 # rad: Angles of rotation in Radians
667 # --------------------------------------------------------------------
668 def rotate_y(v1, rot):
669 v2 = [0, 0, 0]
671 rady = rot[1]
673 # Y axis
674 v2[0] = v1[0] * cos(rady) + v1[2] * sin(rady)
675 v2[1] = v1[1]
676 v2[2] = v1[2] * cos(rady) - v1[0] * sin(rady)
678 return v2
681 # --------------------------------------------------------------------
682 # rotate point EULER Z
683 # v1: point
684 # rad: Angles of rotation in Radians
685 # --------------------------------------------------------------------
686 def rotate_z(v1, rot):
687 v2 = [0, 0, 0]
689 radz = rot[2]
691 # Z axis
692 v2[0] = v1[0] * cos(radz) - v1[1] * sin(radz)
693 v2[1] = v1[0] * sin(radz) + v1[1] * cos(radz)
694 v2[2] = v1[2]
696 return v2