Code clean-up: Use standard-conformal {'sets'} for .report and operator return values
[blender-addons-contrib.git] / geodesic_domes / third_domes_panel.py
1 import bpy
2 import os
3 from geodesic_domes import vefm_259 
4 from geodesic_domes import forms_259
5 from geodesic_domes import geodesic_classes_259
6 from geodesic_domes import add_shape_geodesic
7
8 from bpy.props import EnumProperty, IntProperty, FloatProperty, StringProperty, BoolProperty
9 import math
10 from math import pi
11 from mathutils import Vector #needed to check vertex.vector values
12 try:
13     breakpoint = bpy.types.bp.bp
14 except:
15     print("add breakpoint addon!")
16         
17
18 ########global######
19 last_generated_object = None
20 last_imported_mesh = None
21 basegeodesic = None
22 imported_hubmesh_to_use = None
23 ########global end######
24
25 ########EIND FOR SHAPEKEYS######
26 ### error messages?!
27 bpy.types.Scene.error_message = StringProperty(name="actual error", default = "")    
28
29
30 bpy.types.Scene.geodesic_not_yet_called = BoolProperty(name="geodesic_not_called",default = True)
31
32 bpy.types.Scene.gd_help_text_width = IntProperty(name = "Text Width" , description = "The width above which the text wraps" , default = 60 , max = 180 , min = 20)
33
34 class Geodesic_Domes_Operator_Panel(bpy.types.Panel):
35     """start a GD object here"""
36     bl_label = "Geodesic Domes"
37     bl_region_type = "TOOLS" #UI possible too
38     bl_space_type = "VIEW_3D"
39
40     def draw(self,context):
41         sce = context.scene
42         layout = self.layout
43         col = layout.column()
44         col.label("To start an GD object: ")
45         col.operator(GenerateGeodesicDome.bl_idname,"execute me!")
46             
47 class GenerateGeodesicDome(bpy.types.Operator):
48     bl_label = "Modify Geodesic Objects"
49     bl_idname = "mesh.generate_geodesic_dome"
50     bl_description = "Create object dependent on selection"
51     bl_options = {'REGISTER','UNDO'}
52
53 #PKHG_NEW saving and loading parameters
54     save_parameters = BoolProperty(name = "save params",\
55            description = "activation save */tmp/GD_0.GD", default = False)
56     load_parameters  = BoolProperty(name = "load params",\
57            description = "read */tmp/GD_0.GD", default = False)
58
59     gd_help_text_width = IntProperty(name = "Text Width" , description = "The width above which the text wraps" , default = 60 , max = 180 , min = 20)
60
61     
62     mainpages = EnumProperty(
63     name="Menu",
64     description="Create Faces, Struts & Hubs",
65     items=[("Main","Main","Geodesic objects"),
66            ("Faces","Faces","Generate Faces"),
67            ("Struts","Struts","Generate Struts"),
68            ("Hubs","Hubs","Generate Hubs"),
69            ("Help","Help","Not implemented"),
70           ],
71     default='Main')
72
73 #for Faces!
74     facetype_menu = EnumProperty(
75     name="Faces",
76     description="choose a facetype",
77     items=[("0","strip","strip"),
78            ("1","open vertical","vertical"),
79            ("2","opwn slanted","slanted"),
80            ("3","closed point","closed point"),
81            ("4","pillow","pillow"),
82            ("5","closed vertical","closed vertical"),
83            ("6","stepped","stepped"),
84            ("7","spikes","spikes"),
85            ("8","boxed","boxed"),
86            ("9","diamond","diamond"),
87            ("10","bar","bar"),
88           ],
89     default='0')
90
91     facetoggle = BoolProperty(name="Activate: Face Object", description = "Activate Faces for Geodesic object", default = False )
92 #    faceimporttoggle = BoolProperty(name="faceimporttoggle", default = False )
93     face_use_imported_object = BoolProperty(name="Use: Imported Object",\
94                 description = "Activate faces on your Imported object", default = False)
95     facewidth = FloatProperty(name="facewidth", min = -8,   max = 8, default = .50)
96     fwtog = BoolProperty(name="fwtog", default = False )
97     faceheight = FloatProperty(name="faceheight", min = -8, max = 8, default = 1 )
98     fhtog = BoolProperty(name="fhtog", default = False )
99     face_detach = BoolProperty(name="face_detach", default = False )
100     fmeshname = StringProperty(name="fmeshname", default = "defaultface")
101
102
103     geodesic_types = EnumProperty(
104     name="Objects",
105     description="Choose Geodesic, Grid, Cylinder,Parabola, Torus, Sphere, Import your mesh or Superparameters",
106     items=[("Geodesic","Geodesic","Generate Geodesic"),
107            ("Grid","Grid","Generate Grid"),
108            ("Cylinder","Cylinder","Generate Cylinder"),
109            ("Parabola","Parabola","Generate Parabola"),
110            ("Torus","Torus","Generate Torus"),
111            ("Sphere","Sphere","Generate Sphere"),
112            ("Import your mesh","Import your mesh","Import Your Mesh"),
113           ],
114     default='Geodesic')
115
116     import_mesh_name = StringProperty(name = "mesh to import",\
117             description = "the name has to be the name of a meshobject", default = "None") 
118
119     base_type = EnumProperty(
120     name="Hedron",
121     description="Choose between Tetrahedron, Octahedron, Icosahedron ",
122     items=[("Tetrahedron","Tetrahedron","Generate Tetrahedron"),
123            ("Octahedron","Octahedron","Generate Octahedron"),
124            ("Icosahedron","Icosahedron","Generate Icosahedron"),
125           ],
126     default='Tetrahedron')
127
128     orientation = EnumProperty(
129     name="Point^",
130     description="Point (Vert), Edge or Face pointing upwards",
131     items=[("PointUp","PointUp","Point up"),
132            ("EdgeUp","EdgeUp","Edge up"),
133            ("FaceUp","FaceUp","Face up"),
134            ],
135     default='PointUp')
136
137     geodesic_class = EnumProperty(
138     name="Class",
139     description="Subdivide Basic/Triacon",
140     items=[("Class 1","Class 1","class one"),
141            ("Class 2","Class 2","class two"),
142            ],
143     default='Class 1')
144
145     tri_hex_star = EnumProperty(
146     name="Shape",
147     description="Choose between tri hex star face types",
148     items=[("tri","tri","tri faces"),
149            ("hex","hex","hex faces(by tri)"),
150            ("star","star","star faces(by tri)"),
151               ],
152     default='tri')
153
154     spherical_flat = EnumProperty(
155     name="Round",
156     description="Choose between spherical or flat ",
157     items=[("spherical","spherical","Generate spherical"),
158            ("flat","flat","Generate flat"),
159               ],
160     default='spherical')
161
162     use_imported_mesh = BoolProperty(name="use import",\
163                     description = "Use an imported mesh", default = False)
164
165 #Cylinder
166     cyxres= IntProperty(name="Resolution x/y", min = 3, max = 32,\
167               description = "number of faces around x/y", default = 5 )
168     cyyres= IntProperty(name="Resolution z", min = 3, max = 32,\
169               description = "number of faces in z direction", default = 5 )
170     cyxsz= FloatProperty(name="Scale x/y", min = 0.01, max = 10,\
171                   description = "scale in x/y direction", default = 1 )
172     cyysz= FloatProperty(name="Scale z", min = 0.01, max = 10,\
173               description = "scale in z direction", default = 1 )    
174     cyxell= FloatProperty(name="Stretch x",  min = 0.001, max = 4,\
175               description = "stretch in x direction", default = 1 )
176     cygap= FloatProperty(name="Gap",  min = -2, max = 2, precision = 4,\
177               description = "shrink in % around radius", default = 1 )
178     cygphase= FloatProperty(name="Phase", min = -4, max = 4,\
179               description = "rotate around pivot x/y", default = 0 )
180 #Parabola
181     paxres= IntProperty(name="Resolution x/y",  min = 3, max = 32,\
182            description = "number of faces around x/y", default = 5 )
183     payres= IntProperty(name="Resolution z",  min = 3, max = 32,\
184            description = "number of faces in z direction", default = 5 )
185     paxsz= FloatProperty(name="Scale x/y", min = 0.001, max = 10,\
186            description = "scale in x/y direction", default = 0.30)
187     paysz= FloatProperty(name="Scale z", min = 0.001, max = 10,\
188            description = "scale in z direction",        default = 1 )
189     paxell= FloatProperty(name="Stretch x", min = 0.001, max = 4,\
190            description = "stretch in x direction",      default = 1 )
191     pagap= FloatProperty(name="Gap", min = -2, max = 2,\
192            description = "shrink in % around radius", precision = 4, default = 1 )
193     pagphase= FloatProperty(name="Phase", min = -4, max = 4,\
194            description = "rotate around pivot x/y",     default = 0 )
195 #Torus            
196     ures= IntProperty(name="Resolution x/y",min = 3, max = 32,\
197            description = "number of faces around x/y", default = 8 )
198     vres= IntProperty(name="Resolution z", min = 3, max = 32,\
199             description = "number of faces in z direction", default = 8 )
200     urad= FloatProperty(name="Radius x/y", min = 0.001, max = 10,\
201             description = "radius in x/y plane",        default = 1 )
202     vrad= FloatProperty(name="Radius z", min = 0.001, max = 10,\
203             description = "radius in z plane",  default = 0.250)
204     uellipse= FloatProperty(name="Stretch x", min = 0.001, max = 10,\
205             description = "number of faces in z direction",     default = 1 )
206     vellipse= FloatProperty(name="Stretch z", min = 0.001, max = 10,\
207             description = "number of faces in z direction",     default = 1 )
208     upart= FloatProperty(name="Gap x/y", min = -4, max = 4, precision = 4,\
209             description = "shrink faces around x/y",    default = 1 )
210     vpart= FloatProperty(name="Gap z", min = -4, max = 4,  precision = 4,\
211             description = "shrink faces in z direction",        default = 1 )
212     ugap= FloatProperty(name="Phase x/y",  min = -4, max = 4, precision = 4,\
213            description = "rotate around pivot x/y", default = 0 )
214     vgap= FloatProperty(name="Phase z",  min = -4, max = 4, precision = 4,\
215            description = "rotate around pivot z", default = 0 )
216     uphase= FloatProperty(name="uphase", min = -4, max = 4,\
217             description = "number of faces in z direction",     default = 0 )
218     vphase= FloatProperty(name="vphase",  min = -4, max = 4,\
219             description = "number of faces in z direction",     default = 0 )
220     uexp= FloatProperty(name="uexp",  min = -4, max = 4,\
221             description = "number of faces in z direction",     default = 0 )
222     vexp= FloatProperty(name="vexp",  min = -4, max = 4,\
223             description = "number of faces in z direction",     default = 0 )
224     usuper= FloatProperty(name="usuper", min = -4, max = 4,\
225            description = "first set of superform parameters",  default = 2 )
226     vsuper= FloatProperty(name="vsuper",  min = -4, max = 4,\
227             description = "second set of superform parameters", default = 2 )
228     utwist= FloatProperty(name="Twist x/y", min = -4, max = 4,\
229             description = " use with superformular u",  default = 0 )
230     vtwist= FloatProperty(name="Twist z", min = -4, max = 4,\
231             description = "use with superformular v",   default = 0 )
232
233 #Sphere 
234     bures= IntProperty(name="Resolution x/y", min = 3, max = 32,\
235             description = "number of faces around x/y", default = 8 )
236     bvres= IntProperty(name="Resolution z", min = 3, max = 32,\
237             description = "number of faces in z direction",     default = 8 )
238     burad= FloatProperty(name="Radius",  min = -4, max = 4,\
239             description = "overall radius",     default = 1 )
240     bupart= FloatProperty(name="Gap x/y", min = -4, max = 4, precision = 4,\
241             description = "shrink faces around x/y",    default = 1 )
242     bvpart= FloatProperty(name="Gap z", min = -4, max = 4, precision = 4,\
243             description = "shrink faces in z direction",        default = 1 )
244     buphase= FloatProperty(name="Phase x/y",  min = -4, max = 4,
245             description = "rotate around pivot x/y",    default = 0 )
246     bvphase= FloatProperty(name="Phase z", min = -4, max = 4,\
247             description = "rotate around pivot z",      default = 0 )
248     buellipse= FloatProperty(name="Stretch x", min = 0.001, max = 4,\
249             description = "stretch in the x direction", default = 1 )
250     bvellipse= FloatProperty(name="Stretch z", min = 0.001, max = 4,\
251             description = "stretch in the z direction", default = 1 )
252 #Grid    
253     grxres = IntProperty(name="Resolution x", min = 2, soft_max = 10, max = 20,\
254                          description = "number of faces in x direction", default = 5 )
255     gryres = IntProperty(name="Resolution z",min = 2, soft_min = 2, soft_max=10, max = 20,\
256                          description = "number of faces in x direction", default = 2)
257     grxsz = FloatProperty(name = "X size", min = 1, soft_min=0.01, soft_max=5, max = 10,\
258                          description = "x size", default = 2.0)
259     grysz = FloatProperty(name="Y size",min = 1, soft_min=0.01, soft_max=5, max = 10,\
260                          description = "y size", default = 1.0)
261
262 #PKHG_TODO_??? what means cart
263     cart = IntProperty(name = "cart",min = 0, max = 2,  default = 0)
264                          
265     frequency = IntProperty(name="Frequency", min = 1, max = 8,\
266                            description ="subdivide base triangles", default = 1 )
267     eccentricity = FloatProperty(name = "Eccentricity",  min = 0.01 , max = 4,\
268                  description = "scaling in x/y dimension", default = 1 )
269     squish = FloatProperty(name = "Squish",min = 0.01, soft_max = 4, max = 10,\
270                  description = "scaling in z dimension",  default = 1 )
271     radius = FloatProperty(name = "Radius",min = 0.01, soft_max = 4, max = 10,\
272                  description = "overall radius",  default = 1 )
273     squareness = FloatProperty(name="Square x/y", min = 0.1, max = 5,\
274                  description = "superelipse action in x/y", default = 2 )
275     squarez = FloatProperty(name="Square z", min = 0.1, soft_max = 5, max = 10,\
276                  description = "superelipse action in z", default = 2 )
277     baselevel = IntProperty(name="baselevel", default = 5 )
278     dual = BoolProperty(name="Dual", description = "faces become verts, verts become faces, edges flip", default = False)
279     rotxy = FloatProperty(name="Rotate x/y", min= -4, max = 4,\
280                  description = "rotate superelipse action in x/y", default = 0 )
281     rotz = FloatProperty(name="Rotate z",  min= -4, max = 4,\
282                  description = "rotate superelipse action in z", default = 0 )
283
284 #for choice of superformula
285     uact = BoolProperty(name = 'superformula u (x/y)', description = "activate superformula u parameters", default = False)
286     vact = BoolProperty(name = 'superformula v (z)', description = "activate superformula v parameters", default = False)
287     um = FloatProperty(name = 'um', min = 0, soft_min=0.1, soft_max = 10, max = 20,\
288                  description = "to do", default =  3)
289     un1 = FloatProperty(name = 'un1', min = 0, soft_min=0.1, soft_max = 10,max = 20,\
290                  description = "to do", default =  1)
291     un2 = FloatProperty(name = 'un2', min = 0, soft_min=0.1, soft_max = 10,max = 20,\
292                  description = "to do", default =  1)
293     un3 = FloatProperty(name = 'un3', min = 0,   soft_min=0.1, soft_max = 10, max = 20,\
294                  description = "to do", default =  1)
295     ua = FloatProperty(name = 'ua', min = 0.01, soft_min=0.1, soft_max = 8, max = 16,\
296                  description = "semi-diameter (has both soft pars!)", default =  1.0)
297     ub = FloatProperty(name = 'ub', min = 0.01, soft_min = 0.1, soft_max = 8, max = 16,\
298                  description = "semi-diameter  (has both soft pars!)", default =  1.0)
299     vm = FloatProperty(name = 'vm', min = 0, soft_min=0.1, soft_max=5,max = 10,\
300                  description = "to do", default =  1)
301     vn1 = FloatProperty(name = 'vn1', min = 0, soft_min=0.1, soft_max=5,max = 10,\
302                  description = "to do", default =  1)
303     vn2 = FloatProperty(name = 'vn2', min = 0, soft_min=0.1, soft_max=5,max = 10,\
304                  description = "to do", default =  1)
305     vn3 = FloatProperty(name = 'vn3', min = 0, soft_min=0.1, soft_max=5,max = 10,\
306                  description = "to do", default =  1)
307     va = FloatProperty(name = 'va', min = 0, soft_min=0.1, soft_max=5,max = 10,\
308                  description = "to do", default =  1)
309     vb = FloatProperty(name = 'vb', min = 0, soft_min=0.1, soft_max=5,max = 10,\
310                  description = "to do", default =  1)
311
312     uturn = FloatProperty(name = 'uturn', min = -5, soft_min=0, soft_max=5,max = 10,\
313                  description = "to do", default =  0)
314     vturn = FloatProperty(name = 'vturn', min = 0, soft_min=0.1, soft_max=5,max = 10,\
315                  description = "to do", default =  0)
316     utwist = FloatProperty(name = 'utwist', min = 0, soft_min=0.1, soft_max=5,max = 10,\
317                  description = "to do", default =  0)
318     vtwist = FloatProperty(name = 'vtwist', min = 0, soft_min=0.1, soft_max=5,max = 10,\
319                  description = "to do", default =  0)
320
321 #Strut
322     struttype= IntProperty(name="struttype", default= 0)
323     struttoggle = BoolProperty(name="struttoggle", default = False )
324     strutimporttoggle= BoolProperty(name="strutimporttoggle", default = False )
325     strutimpmesh= StringProperty(name="strutimpmesh", default = "None")
326     strutwidth= FloatProperty(name="strutwidth", min = -10, soft_min = 5, soft_max = 5, max = 10, default = 1 )
327     swtog= BoolProperty(name="swtog", default = False )
328     strutheight= FloatProperty(name="strutheight", min = -5, soft_min = -1, soft_max = 5, max = 10, default = 1 )
329     shtog= BoolProperty(name="shtog", default = False )
330     strutshrink= FloatProperty(name="strutshrink", min = 0.001, max = 4, default = 1 )
331     sstog= BoolProperty(name="sstog", default = False )
332     stretch= FloatProperty(name="stretch",  min= -4, max = 4, default = 1.0 )
333     lift= FloatProperty(name="lift", min = 0.001, max = 10, default = 0 )
334     smeshname= StringProperty(name="smeshname", default = "defaultstrut")
335
336 #Hubs
337     hubtype = BoolProperty(name ="hubtype", description = "not used", default = True )
338     hubtoggle = BoolProperty(name ="hubtoggle", default = False )
339     hubimporttoggle = BoolProperty(name="new import", description = "import a mesh", default = False )
340     hubimpmesh = StringProperty(name="hubimpmesh",\
341                  description = "name of mesh to import",  default = "None")
342     hubwidth = FloatProperty(name="hubwidth", min = 0.01, max = 10,\
343                  default = 1 )
344     hwtog = BoolProperty(name="hwtog", default = False )
345     hubheight = FloatProperty(name="hubheight", min = 0.01, max = 10,\
346                  default = 1 )
347     hhtog = BoolProperty(name="hhtog", default = False )
348     hublength = FloatProperty(name ="hublength", min  = 0.1, max  = 10,\
349                  default  = 1 )
350     hstog= BoolProperty(name="hstog", default = False )
351     hmeshname= StringProperty(name="hmeshname", description = "Name of an existing mesh needed!", default = "None")
352
353     name_list = ['facetype_menu','facetoggle','face_use_imported_object',
354 'facewidth','fwtog','faceheight','fhtog',
355 'face_detach','fmeshname','geodesic_types','import_mesh_name',
356 'base_type','orientation','geodesic_class','tri_hex_star',
357 'spherical_flat','use_imported_mesh','cyxres','cyyres',
358 'cyxsz','cyysz','cyxell','cygap',
359 'cygphase','paxres','payres','paxsz',
360 'paysz','paxell','pagap','pagphase',
361 'ures','vres','urad','vrad',
362 'uellipse','vellipse','upart','vpart',
363 'ugap','vgap','uphase','vphase',
364 'uexp','vexp','usuper','vsuper',
365 'utwist','vtwist','bures','bvres',
366 'burad','bupart','bvpart','buphase',
367 'bvphase','buellipse','bvellipse','grxres',
368 'gryres','grxsz','grysz',
369 'cart','frequency','eccentricity','squish',
370 'radius','squareness','squarez','baselevel',
371 'dual','rotxy','rotz',
372 'uact','vact','um','un1',
373 'un2','un3','ua','ub',
374 'vm','vn1','vn2','vn3',
375 'va','vb','uturn','vturn',
376 'utwist','vtwist','struttype','struttoggle',
377 'strutimporttoggle','strutimpmesh','strutwidth','swtog',
378 'strutheight','shtog','strutshrink','sstog',
379 'stretch','lift','smeshname','hubtype',
380 'hubtoggle','hubimporttoggle','hubimpmesh','hubwidth',
381 'hwtog','hubheight','hhtog','hublength',
382 'hstog','hmeshname']    
383
384     def write_params(self,filename):
385         file = open(filename, "w", encoding="utf8", newline="\n")
386         fw = file.write
387     #for Faces!
388         for el in self.name_list:
389             fw(el + ",")
390             fw(repr(getattr(self,el)))
391             fw(",\n")
392         file.close()
393
394     def read_file(self,filename):
395         file = open(filename, "r", newline="\n")
396         result = []
397         line = file.readline()
398         while(line):
399             tmp = line.split(",")
400             result.append(eval(tmp[1]))
401             line = file.readline()
402         return result
403     
404
405     def draw(self,context):
406         sce = context.scene
407         layout = self.layout
408         row = layout.row()
409         row.prop(self,"save_parameters")
410         row.prop(self,"load_parameters")
411         col = layout.column()
412         col.label(" ")
413         col.prop(self,"mainpages")
414         which_mainpages = self.mainpages
415         if which_mainpages == 'Main':
416             col = layout.column()
417             col.prop(self,"geodesic_types")
418             tmp = self.geodesic_types
419             if tmp == "Geodesic":
420                 col.label(text="Geodesic Object Types:")
421                 col.prop(self, "geodesic_class")                
422                 col.prop(self, "base_type")
423                 col.prop(self, "orientation")
424                 col.prop(self, "tri_hex_star")
425                 col.prop(self, "spherical_flat")
426                 col.label("Geodesic Object Parameters:")
427                 row = layout.row()
428                 row.prop(self,"frequency")
429                 row = layout.row()
430                 row.prop(self,"radius")
431                 row = layout.row()
432                 row.prop(self,"eccentricity")
433                 row = layout.row()
434                 row.prop(self,"squish")
435                 row = layout.row()
436                 row.prop(self,"squareness")
437                 row = layout.row()
438                 row.prop(self,"squarez")
439                 row = layout.row()
440                 row.prop(self,"rotxy")
441                 row = layout.row()
442                 row.prop(self,"rotz")
443                 row = layout.row()
444                 row.prop(self,"dual")
445             elif tmp == 'Torus':
446                 col.label("Torus Parameters")
447                 row = layout.row()
448                 row.prop(self, "ures")
449                 row = layout.row()
450                 row.prop(self, "vres")
451                 row = layout.row()
452                 row.prop(self, "urad")
453                 row = layout.row()
454                 row.prop(self, "vrad")
455                 row = layout.row()
456                 row.prop(self, "uellipse")
457                 row = layout.row()
458                 row.prop(self, "vellipse")
459                 row = layout.row()
460                 row.prop(self, "upart")
461                 row = layout.row()
462                 row.prop(self, "vpart")
463                 row = layout.row()
464                 row.prop(self, "ugap")
465                 row.prop(self, "vgap")
466                 row = layout.row()
467                 row.prop(self, "uphase")
468                 row.prop(self, "vphase")
469                 row = layout.row()
470                 row.prop(self, "uexp")
471                 row.prop(self, "vexp")
472                 row = layout.row()
473                 row.prop(self, "usuper")
474                 row.prop(self, "vsuper")
475                 row = layout.row()
476                 row.prop(self, "vgap")
477                 row = layout.row()
478             elif tmp == 'Sphere':
479                 col.label("Sphere Parameters")
480                 row = layout.row()
481                 row.prop(self,"bures")
482                 row = layout.row()
483                 row.prop(self,"bvres")
484                 row = layout.row()
485                 row.prop(self,"burad")
486                 row = layout.row()                
487                 row.prop(self,"bupart")
488                 row = layout.row()
489                 row.prop(self,"buphase")
490                 row = layout.row()
491                 row.prop(self,"bvpart")
492                 row = layout.row()
493                 row.prop(self,"bvphase")
494                 row = layout.row()
495                 row.prop(self,"buellipse")
496                 row = layout.row()
497                 row.prop(self,"bvellipse")
498             elif tmp == 'Parabola':
499                 col.label("Parabola Parameters")
500                 row = layout.row()
501                 row.prop(self, "paxres")
502                 row = layout.row()
503                 row.prop(self, "payres")
504                 row = layout.row()
505                 row.prop(self, "paxsz")
506                 row = layout.row()
507                 row.prop(self, "paysz")
508                 row = layout.row()
509                 row.prop(self, "paxell")
510                 row = layout.row()
511                 row.prop(self, "pagap")
512                 row = layout.row()
513                 row.prop(self, "pagphase")
514             elif tmp == 'Cylinder':
515                 col.label("Cylinder Parameters")
516                 col.prop(self, "cyxres")
517                 col.prop(self, "cyyres")
518                 col.prop(self, "cyxsz")
519                 col.prop(self, "cyysz")
520                 col.prop(self, "cyxell")
521                 col.prop(self, "cygap")
522                 col.prop(self, "cygphase")
523             elif tmp == 'Grid':
524                 col.label("Grid Parameters")
525                 row = layout.row()
526                 row.prop(self, "grxres")
527                 row = layout.row()
528                 row.prop(self, "gryres")
529                 row = layout.row()
530                 row.prop(self, "grxsz")
531                 row = layout.row()
532                 row.prop(self, "grysz")
533             elif tmp == 'Import your mesh':
534                 col.prop(self,"use_imported_mesh")
535                 col.prop(self, "import_mesh_name")
536 #######superform parameters only where possible
537             row = layout.row()
538             row.prop(self,"uact")
539             row = layout.row()
540             row.prop(self,"vact")                
541             row = layout.row()
542             if not(tmp == 'Import your mesh'):
543                 if (self.uact == False) and (self.vact == False):
544                     row.label("no checkbox active!")
545                 else:
546                     row.label("Superform Parameters")
547                 if self.uact:
548                     row = layout.row()
549                     row.prop(self,"um")
550                     row = layout.row()
551                     row.prop(self,"un1")
552                     row = layout.row()
553                     row.prop(self,"un2")
554                     row = layout.row()
555                     row.prop(self,"un3")
556                     row = layout.row()
557                     row.prop(self,"ua")
558                     row = layout.row()
559                     row.prop(self,"ub")
560                     row = layout.row()
561                     row.prop(self,"uturn")
562                     row = layout.row()
563                     row.prop(self,"utwist")
564                 if self.vact:
565                     row = layout.row()
566                     row.prop(self,"vm")
567                     row = layout.row()
568                     row.prop(self,"vn1")
569                     row = layout.row()
570                     row.prop(self,"vn2")
571                     row = layout.row()
572                     row.prop(self,"vn3")
573                     row = layout.row()
574                     row.prop(self,"va")
575                     row = layout.row()
576                     row.prop(self,"vb")
577                     row = layout.row()
578                     row.prop(self,"vturn")
579                     row = layout.row()
580                     row.prop(self,"vtwist")                
581 ########einde superfo
582         elif  which_mainpages == "Hubs":
583             row = layout.row()
584             row.prop(self, "hubtoggle")
585 #PKHG_NOT_USDED_YET_24-11            row.prop(self, "hubtype")
586             row = layout.row()            
587 #25-11 not needed            row.prop(self, "hubimporttoggle")
588             row = layout.row()
589             if self.hubimpmesh == "None":
590                 row = layout.row()
591                 row.label("name of a hub to use")
592                 row = layout.row()
593             row.prop(self, "hubimpmesh")
594             row = layout.row()
595             if self.hmeshname == "None":
596                 row = layout.row()
597                 row.label("name of mesh to be filled in!")
598                 row = layout.row()
599             row.prop(self,"hmeshname")
600             row = layout.row()
601             row.prop(self, "hwtog")
602             if self.hwtog:
603                 row.prop(self, "hubwidth")
604             row = layout.row()
605             row.prop(self, "hhtog")
606             if self.hhtog:
607                 row.prop(self, "hubheight")
608             row = layout.row()
609             row.prop(self, "hublength")                
610         elif which_mainpages == "Struts":
611             row = layout.row()
612 #            row.prop(self, "struttype")
613             row.prop(self, "struttoggle")
614 #            row = layout.row()            
615 #            row.prop(self, "strutimporttoggle")
616             row = layout.row()            
617             row.prop(self, "strutimpmesh")
618             row = layout.row()
619             row.prop(self, "swtog")
620             if self.swtog:
621                 row.prop(self, "strutwidth")
622             row = layout.row()
623             row.prop(self, "shtog")
624             if self.shtog:
625                 row.prop(self, "strutheight")
626             row = layout.row()
627             row.prop(self, "sstog")
628             if self.sstog:
629                row.prop(self, "strutshrink")
630             row = layout.row()               
631             row.prop(self, "stretch")
632             row = layout.row()            
633             row.prop(self, "lift")
634             row = layout.row()            
635             row.prop(self, "smeshname")
636         elif which_mainpages == "Faces":
637             row = layout.row()
638             row.prop(self,"facetoggle")
639             row = layout.row()
640             row.prop(self,"face_use_imported_object")
641             row = layout.row()
642             row.prop(self,"facetype_menu")
643             row = layout.row()
644             row.prop(self,"fwtog")
645             if self.fwtog:
646                 row.prop(self,"facewidth")
647             row = layout.row()
648             row.prop(self,"fhtog")
649             if self.fhtog:
650                 row.prop(self,"faceheight")
651             row = layout.row()
652             row.prop(self,"face_detach")
653             row = layout.row()
654             row.prop(self,"fmeshname")
655             row = layout.row()
656         
657         #help menu GUI
658         elif which_mainpages == "Help":
659             import textwrap
660
661             # a function that allows for multiple labels with text that wraps
662             # you can allow the user to set where the text wraps with the 
663             # text_width parameter
664             # other parameters are ui : here you usually pass layout
665             # text: is a list with each index representing a line of text
666             
667             def multi_label(text, ui,text_width=120):
668                 for x in range(0,len(text)):
669                     el = textwrap.wrap(text[x], width = text_width )
670             
671                     for y in range(0,len(el)):
672                         ui.label(text=el[y])
673
674             box = layout.box() 
675             help_text = ["NEW! ", 
676                 "New facility: save or load (nearly all) parameters ",
677                  "A file GD_0.GD will be used, living in: ",
678                  "geodesic_domes/tmp ",
679                  "and if possible two backups "
680                  "--------",
681                  "After loading you have to change a ",
682                  "parameter back and forth "
683                  "to see it"]
684             text_width = self.gd_help_text_width
685             box.prop(self,"gd_help_text_width",slider=True)
686             multi_label(help_text,box, text_width)
687
688     def execute(self, context):
689         global last_generated_object, last_imported_mesh, basegeodesic, imported_hubmesh_to_use 
690         #default superformparam = [3, 10, 10, 10, 1, 1, 4, 10, 10, 10, 1, 1, 0, 0, 0.0, 0.0, 0, 0]]
691         superformparam = [self.um, self.un1, self.un2, self.un3, self.ua,\
692                           self.ub, self.vm, self.vn1, self.vn2, self.vn3,\
693                           self.va, self.vb, self.uact, self.vact,\
694                           self.uturn*pi, self.vturn*pi, \
695                           self.utwist, self.vtwist]
696         context.scene.error_message = "" 
697         if self.mainpages == 'Main':
698             if self.geodesic_types == "Geodesic":
699                 tmp_fs = self.tri_hex_star
700                 faceshape = 0 # tri!
701                 if tmp_fs == "hex":
702                     faceshape = 1
703                 elif tmp_fs == "star":
704                     faceshape = 2
705                 tmp_cl = self.geodesic_class
706                 klass = 0
707                 if tmp_cl == "Class 2":
708                     klass = 1
709                 shape = 0 
710                 parameters = [self.frequency, self.eccentricity, self.squish,\
711                           self.radius, self.squareness, self.squarez, 0,\
712                           shape, self.baselevel, faceshape, self.dual,\
713                           self.rotxy, self.rotz, klass, superformparam]               
714                 basegeodesic =  creategeo(self.base_type,self.orientation,parameters)
715                 basegeodesic.makegeodesic()
716                 basegeodesic.connectivity()
717                 mesh = vefm_259.mesh()                
718                 vefm_259.finalfill(basegeodesic,mesh) #always! for hexifiy etc. necessarry!!!                     
719                 vefm_259.vefm_add_object(mesh)
720                 last_generated_object = context.active_object
721                 last_generated_object.location  = (0,0,0)
722                 context.scene.objects.active = last_generated_object            
723             elif self.geodesic_types == 'Grid':
724                 basegeodesic = forms_259.grid(self.grxres,self.gryres,\
725                        self.grxsz,self.grysz,1.0,1.0,0,0,0,\
726                                       0,1.0,1.0,superformparam)
727                 vefm_259.vefm_add_object(basegeodesic)
728                 bpy.data.objects[-1].location = (0,0,0)
729             elif self.geodesic_types == "Cylinder":
730                 basegeodesic = forms_259.cylinder(self.cyxres, self.cyyres, \
731                                    self.cyxsz, self.cyysz, self.cygap, \
732                                    1.0, self.cygphase, 0, 0, 0, self.cyxell,\
733                                    1.0, superformparam)
734                 vefm_259.vefm_add_object(basegeodesic)
735                 bpy.data.objects[-1].location = (0,0,0)                
736                 
737             elif self.geodesic_types == "Parabola":
738                 basegeodesic=forms_259.parabola(self.paxres, self.payres,\
739                     self.paxsz, self.paysz, self.pagap,1.0, self.pagphase,\
740                     0,0,0, self.paxell,1.0,superformparam)
741                 vefm_259.vefm_add_object(basegeodesic)
742                 bpy.data.objects[-1].location = (0,0,0)                
743             elif self.geodesic_types == "Torus":
744                 basegeodesic = forms_259.torus(self.ures, self.vres,\
745                        self.vrad, self.urad, self.upart, self.vpart,\
746                        self.ugap, self.vgap,0,0, self.uellipse,\
747                        self.vellipse, superformparam)
748                 vefm_259.vefm_add_object(basegeodesic)
749                 bpy.data.objects[-1].location = (0,0,0)
750             elif self.geodesic_types == "Sphere":
751                 basegeodesic=forms_259.sphere(self.bures, self.bvres,\
752                         self.burad,1.0, self.bupart, self.bvpart,\
753                         self.buphase, self.bvphase,0,0, self.buellipse,\
754                         self.bvellipse,superformparam)  
755
756                 vefm_259.vefm_add_object(basegeodesic)
757                 bpy.data.objects[-1].location = (0,0,0)                
758             elif self.geodesic_types == "Import your mesh":
759                 obj_name = self.import_mesh_name
760                 if obj_name == "None":
761                     message = "fill in a name \nof an existing mesh\nto be imported"
762                     context.scene.error_message = message
763                     self.report({'INFO'}, message)
764                     print("***INFO*** you have to fill in the name of an existing mesh")
765                 else:
766 #                    obj_in_scene = context.objects
767                     names = [el.name for el in context.scene.objects]
768                     if obj_name in names and context.scene.objects[obj_name].type == "MESH":
769                         obj = context.scene.objects[obj_name]
770 #                        if obj.type == "MESH":                        
771                         your_obj = vefm_259.importmesh(obj.name,False)
772                         last_imported_mesh = your_obj
773                         vefm_259.vefm_add_object(your_obj)
774                         last_generated_object = bpy.context.active_object
775                         last_generated_object.name ="Imported mesh"
776                         bpy.context.active_object.location = (0,0,0)
777                     else:
778                         message = obj_name +" does not exist \nor is not a MESH"
779                         context.scene.error_message = message                
780                         bpy.ops.object.dialog_operator('INVOKE_DEFAULT')
781                         self.report({'ERROR'}, message)
782                         print("***ERROR***" + obj_name +" does not exist or is not a MESH")
783         elif self.mainpages == "Hubs":
784             hubtype = self.hubtype
785             hubtoggle =  self.hubtoggle
786             hubimporttoggle = self.hubimporttoggle
787             hubimpmesh = self.hubimpmesh
788             hubwidth = self.hubwidth
789             hwtog = self.hwtog
790             hubheight = self.hubheight
791             hhtog = self.hhtog
792             hublength = self.hublength
793             hstog =  self.hstog
794             hmeshname=  self.hmeshname
795 #PKHG_TODO_27-11 better info!??
796             if not (hmeshname == "None") and not (hubimpmesh == "None") and  hubtoggle:                
797                 try:                    
798                     hub_obj = vefm_259.importmesh(hmeshname,0)
799                     hub = vefm_259.hub(hub_obj, True,\
800                             hubwidth, hubheight, hublength,\
801                               hwtog, hhtog, hstog, hubimpmesh)
802                     mesh = vefm_259.mesh("test")
803                     vefm_259.finalfill(hub,mesh)
804                     vefm_259.vefm_add_object(mesh)
805                     bpy.data.objects[-1].location = (0,0,0)
806                 except:
807                     message = "***ERROR*** \neither no mesh for hub\nor " + hmeshname +  " available"                    
808                     context.scene.error_message = message
809                     bpy.ops.object.dialog_operator('INVOKE_DEFAULT')
810                     print(message)                
811             else:
812                 message = "***INFO***\nuse the hub toggle!"
813                 context.scene.error_message = message
814                 print("\n***INFO*** use the hub toggle!")
815         elif self.mainpages == "Struts":
816             struttype = self.struttype
817             struttoggle = self.struttoggle
818             strutimporttoggle = self.strutimporttoggle
819             strutimpmesh = self.strutimpmesh
820             strutwidth = self.strutwidth
821             swtog = self.swtog
822             strutheight = self.strutheight
823             shtog = self.shtog
824             strutshrink = self.strutshrink
825             sstog = self.sstog
826             stretch = self.stretch
827             lift = self.lift
828             smeshname = self.smeshname
829             if not (strutimpmesh == "None") and struttoggle:
830                 names = [el.name for el in context.scene.objects]
831                 if strutimpmesh in names and context.scene.objects[strutimpmesh].type == "MESH":
832                     strut = vefm_259.strut(basegeodesic, struttype,strutwidth,strutheight,stretch,swtog,shtog,swtog,strutimpmesh,sstog,lift)
833                     strutmesh = vefm_259.mesh()
834                     vefm_259.finalfill(strut,strutmesh)
835                     vefm_259.vefm_add_object(strutmesh)
836                     last_generated_object = context.active_object
837                     last_generated_object.name = smeshname
838                     last_generated_object.location  = (0,0,0)
839                 else:
840                     message = "***ERROR***\n" + strutimpmesh + "\nis not a MESH"
841                     context.scene.error_message = message
842                     bpy.ops.object.dialog_operator('INVOKE_DEFAULT')
843                     print("***ERROR*** strut obj is not a MESH")
844             else:
845                 vefm_259.vefm_add_object(basegeodesic)
846         elif self.mainpages == "Faces":
847             if self.facetoggle:
848                 faceparams=[[self.face_detach,0,[[0.5,0.0]]], #0 strip
849                             [self.face_detach,0,[[0.0,0.5]]], #1 vertical
850                             [self.face_detach,0,[[0.5,0.5]]], #2 slanted
851                             [self.face_detach,1,[[0.25,0.25],[0.5,0.5]]], #3 closed point
852                             [self.face_detach,1,[[0.1,0.03],[0.33,0.06],[0.0,0.1]]], #4 pillow
853                             [self.face_detach,2,[[0.0,0.5]]], #5 closed vertical
854                             [self.face_detach,2,[[0.0,0.25],[0.25,0.25],[0.25,0.5]]], #6 stepped
855                             [self.face_detach,1,[[0.2,0.1],[0.4,0.2],[0.0,1.0]]], #7 spikes
856                             [self.face_detach,3,[[0.25,0.0],[0.25,0.5],[0.0,0.5]]], #8 boxed 
857                             [self.face_detach,3,[[0.25,0.5],[0.5,0.0],[0.25,-0.5]]], #9 diamond
858                             [self.face_detach,4,[[0.5,0.0],[0.5,0.5],[0.0,0.5]]],] #10 bar
859                 facedata = faceparams[int(self.facetype_menu)]
860                 if not self.face_use_imported_object:
861                     faceobject = vefm_259.facetype(basegeodesic,facedata,self.facewidth,self.faceheight,self.fwtog)
862                 else:
863                     if last_imported_mesh: 
864                         faceobject = vefm_259.facetype(last_imported_mesh, facedata,self.facewidth,self.faceheight,self.fwtog)
865                     else:
866                         message = "***ERROR***\nno imported message available\n" + "last geodesic used"
867                         self.face_use_imported_object = False
868                         context.scene.error_message = message
869                         bpy.ops.object.dialog_operator('INVOKE_DEFAULT')
870                         print("\n***ERROR*** no imported mesh available")
871                         print("last geodesic used!")
872                         faceobject = vefm_259.facetype(basegeodesic,facedata,\
873                                      self.facewidth,self.faceheight,self.fwtog)
874                 facemesh = vefm_259.mesh()
875                 finalfill(faceobject,facemesh)
876
877                 vefm_259.vefm_add_object(facemesh)
878                 obj = bpy.data.objects[-1]
879                 obj.name = self.fmeshname
880                 obj.location = (0,0,0)
881 #PKHG save or load (nearly) all parameters                
882         if self.save_parameters:
883             self.save_parameters = False
884             try:
885                 print("DBG L884")
886                 message = ""
887                 scriptpath = bpy.utils.script_paths()[0]
888                 sep = os.path.sep
889                 tmpdir = os.path.join(scriptpath,"addons", "geodesic_domes" , "tmp")
890 #scriptpath + sep + "addons" + sep + "geodesic_domes" + sep + "tmp"
891                 print("tmpdirL890 = ",tmpdir)
892                 if not os.path.isdir(tmpdir):
893                     message += "***ERROR***\n" + tmpdir + "\nnot (yet) available\n"
894                     print(message)
895                 else:
896                     filename = tmpdir + sep + "GD_0.GD"
897                     filename_ren = tmpdir + sep + "GD_0.GD.bak"
898                     filename_ren2 = tmpdir + sep + "GD_0.GD.2bak"
899                     if os.path.isfile(filename_ren2):
900                         try:
901                             os.remove(filename_ren2)
902                             message = "***Info***\nGD_0.GD.2bak removed\n"
903                         except:
904                             message = "***ERROR***\n,GD_0.GD.2bak could not be removed\n"
905                         print(message)
906                     if os.path.isfile(filename_ren):
907                         try:
908                             os.rename(filename_ren,filename_ren2)
909                             message += "***INFO***\nGD_0.GD.bak renamed into GD_0.GD.2bak\n"
910                         except:
911                             message += "***Info***\nrenaming GD_0.GD.bak not possible\n"
912                     if os.path.isfile(filename):
913                         try:
914                             os.rename(filename,filename_ren)
915                             message += "***INFO***\nGD_0.GD renamed into GD_0.GD.bak\n"
916                         except:
917                             message += "***ERROR***\ncreation of GD_0.GD.bak not possible\n"
918                     try:
919                         print("DBG L921")
920                         self.write_params(filename)
921                         message += "***OK***\nparameters saved in\n" + filename
922                         print(message)
923                     except:
924                         message = "***ERRROR***\n" + "writing " + filename + "\nnot possible"
925                         print(message)                 
926             except:
927                 
928                 message += "***ERROR***\n Contakt PKHG, something wrong happened"
929                 print(message)
930                 
931             context.scene.error_message = message
932             bpy.ops.object.dialog_operator('INVOKE_DEFAULT')
933
934         if self.load_parameters:
935             self.load_parameters = False
936             try:
937                 scriptpath = bpy.utils.script_paths()[0]
938                 sep = os.path.sep
939                 tmpdir = os.path.join(scriptpath,"addons", "geodesic_domes" , "tmp")
940 #scriptpath + sep + "addons" + sep + "geodesic_domes" + sep + "tmp"                              
941                 if not os.path.isdir(tmpdir):
942                     message = "***ERROR***\n" + tmpdir + "\nnot available"  
943                     print(message)
944                 filename = tmpdir + sep + "GD_0.GD"
945 #        self.read_file(filename)
946                 try:
947                     res = self.read_file(filename)
948                     for i,el in enumerate(self.name_list):
949                         setattr(self,el,res[i])
950                         
951                     message = "***OK***\nparameters read from\n" + filename
952                     print(message)
953                     
954                 except:
955                     message = "***ERRROR***\n" + "writing " + filename + "\nnot possible"
956                 #bpy.context.scene.instant_filenames = filenames
957                 
958             except:
959                 message = "***ERROR***\n Contakt PKHG,\nsomething went wrong reading params happened"
960             context.scene.error_message = message
961             bpy.ops.object.dialog_operator('INVOKE_DEFAULT')
962         return {'FINISHED'}
963     
964     def invoke(self, context, event):
965         global basegeodesic
966         bpy.ops.view3d.snap_cursor_to_center()
967         tmp = context.scene.geodesic_not_yet_called
968         if tmp:            
969             context.scene.geodesic_not_yet_called = False
970         self.execute(context)
971         return {'FINISHED'}
972
973 def creategeo(polytype,orientation,parameters):
974     geo = None
975     if polytype == "Tetrahedron":
976         if orientation == "PointUp":
977             geo = geodesic_classes_259.tetrahedron(parameters)
978         elif orientation == "EdgeUp":
979             geo=geodesic_classes_259.tetraedge(parameters)
980         elif orientation == "FaceUp":
981             geo=geodesic_classes_259.tetraface(parameters)
982     elif polytype == "Octahedron": 
983         if orientation == "PointUp":
984             geo=geodesic_classes_259.octahedron(parameters)
985         elif orientation == "EdgeUp":
986             geo=geodesic_classes_259.octaedge(parameters)
987         elif orientation == "FaceUp":
988             geo=geodesic_classes_259.octaface(parameters)
989     elif polytype == "Icosahedron":
990         if orientation == "PointUp":
991             geo=geodesic_classes_259.icosahedron(parameters)
992         elif orientation == "EdgeUp":
993             geo=geodesic_classes_259.icoedge(parameters)
994         elif orientation == "FaceUp":
995             geo=geodesic_classes_259.icoface(parameters)
996     return geo
997     
998 basegeodesic,fmeshname,smeshname,hmeshname,outputmeshname,strutimpmesh,hubimpmesh = [None]*7
999
1000 def finalfill(source,target):
1001     count=0
1002     for point in source.verts:
1003         newvert = vefm_259.vertex(point.vector)
1004         target.verts.append(newvert)
1005         point.index = count
1006         count  += 1 
1007     for facey in source.faces:
1008         row=len(facey.vertices)
1009         if row >= 5:
1010             newvert = vefm_259.average(facey.vertices).centroid()
1011             centre = vefm_259.vertex(newvert.vector)
1012             target.verts.append(centre)
1013             for i in range(row):
1014                 if i == row - 1:
1015                     a = target.verts[facey.vertices[-1].index]
1016                     b = target.verts[facey.vertices[0].index]
1017                 else:
1018                     a = target.verts[facey.vertices[i].index]
1019                     b = target.verts[facey.vertices[i+1].index]
1020                 c = centre
1021                 f = [a,b,c]
1022                 target.faces.append(f)
1023         else:
1024             f = []
1025             for j in range(len(facey.vertices)):
1026
1027                 a = facey.vertices[j]
1028                 f.append(target.verts[a.index])
1029             target.faces.append(f)
1030         
1031 ###for error messages         
1032 class DialogOperator(bpy.types.Operator):
1033     bl_idname = "object.dialog_operator"
1034     bl_label = "INFO"
1035
1036     def draw(self,context):
1037         layout = self.layout
1038         message = context.scene.error_message
1039         col = layout.column()
1040         tmp = message.split("\n")
1041         for el in tmp:
1042             col.label(el)
1043         
1044     def execute(self, context):
1045         return {'FINISHED'}
1046
1047     def invoke(self, context, event):
1048         wm = context.window_manager
1049         return wm.invoke_props_dialog(self)
1050             
1051
1052 ######### register all
1053 def register():
1054     bpy.utils.register_module(__name__)
1055
1056 def unregister():
1057     bpy.utils.unregister_module(__name__)
1058
1059 if __name__ == "__main__":
1060     register()
1061