Changeset 1447 in main for trunk


Ignore:
Timestamp:
07/12/12 12:43:20 (9 years ago)
Author:
pcosquer
Message:

doc3D: rewrites code to generate 3D menu

Location:
trunk/openPLM/apps/document3D
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/openPLM/apps/document3D/arborescense.py

    r1205 r1447  
    1 # TODO: refactor this code 
    21 
    3 def generate_javascript_for_3D(product): 
    4     """ 
    5      
    6     :param product: :class:`.Product` that represents the arborescense of the :class:`.DocumentFile` that we want to show 
    7      
    8     From a :class:`.Product`  generates the code javascript necessarily to locate and to show the different geometries that compose the visualization 3D 
    9      
    10     """ 
    11     if product: 
    12         numeration=[0] 
    13         javascript=['var object3D = new THREE.Object3D(); var part_to_object = {}; var part_to_parts = {};\n']         
    14         javascript_menu=['function menu() {\nelement = document.createElement("div");\nelement.id="root";\nelement.innerHTML ="'] 
    15          
    16         javascript[0]+='var part%s=new THREE.Object3D();\n'%numeration[0] 
    17         javascript_menu[0]+=function_generate_menu(numeration[0],product.name)          
     2class JSGenerator(object): 
    183 
    19         generate_javascript(product,numeration,javascript,[],javascript_menu,numeration[0]) 
    20          
    21         javascript_menu[0]+='</li></ul>'  
    22         javascript_menu[0]+='";\ndocument.getElementById("menu_").appendChild(element);\n}\n' 
    23  
    24         return javascript_menu[0]+javascript[0] 
    25     else: 
    26         return False 
    27      
    28         
    29 def generate_javascript(product,numeration,javascript,loc,javascript_menu,old_numeration): 
    30     numeration[0]+=1      
    31  
    32     javascript_menu[0]+="<ul>" 
    33          
    34     if product.geometry==False: 
    35         parts_generated=[] 
    36               
    37         for link in product.links: 
    38             for i in range(link.quantity): 
    39  
    40                 loc2=loc[:] 
    41                 loc2.append(link.locations[i]) 
    42                 parts_generated.append(numeration[0]) 
    43                                   
    44                 javascript_menu[0]+=function_generate_menu(numeration[0],link.names[i])                 
    45                        
    46                 generate_javascript(link.product,numeration,javascript,loc2,javascript_menu,numeration[0])  
    47         
    48                 javascript_menu[0]+="</li>" 
    49              
    50         javascript[0]+=generate_functions_visibilty_parts(old_numeration,parts_generated) 
    51      
    52     else:        
    53    
    54         javascript[0]+=generate_functions_visibilty_object(old_numeration,numeration[0],product,loc) 
    55     javascript_menu[0]+="</ul>"         
    56  
    57  
    58  
    59 def generate_functions_visibilty_parts(numeration,parts_generated): 
    60     parts_definition = [] 
    61     function = [function_head % locals()] 
    62     part_to_parts = ['part_to_parts["part%s"] = [' % numeration] 
    63     for part_numeration_child in parts_generated: 
    64         parts_definition.append( "var part%s=new THREE.Object3D();\n"%part_numeration_child) 
    65         function.append(function_change_part % locals()) 
    66         part_to_parts.append('"part%s", ' % part_numeration_child) 
    67     part_to_parts.append("];\n") 
    68     function.append("}\n") 
    69     return ''.join(parts_definition) + ''.join(function) + ''.join(part_to_parts) 
    70   
    71 def generate_functions_visibilty_object(numeration,object_numeration,product,loc): 
    72     reference=product.geometry 
    73     part_id=str(product.doc_id)         
    74                                             
    75     function= function_head % locals() + function_change_object % locals() + "}\n" 
    76     function += "part_to_object['part%(numeration)s'] = object%(object_numeration)s;" % locals() 
    77    
    78     return generate_object(loc,object_numeration,reference,part_id)+function      
    79  
    80  
    81 def generate_object(loc,numeration,reference,part_id): 
    82  
    83     locate="var object%s=new THREE.Mesh(_%s_%s,material_for_%s_%s );\n"%(numeration,reference,part_id,reference,part_id) 
    84     locate+="object%s.matrixAutoUpdate = false;\n"%numeration 
    85     if len(loc)>0: 
    86         for g in range(len(loc)): 
    87             locate+="object%s.matrix.multiplySelf(new THREE.Matrix4(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,0,0,0,1));\n"%(numeration,loc[g].x1,loc[g].x2,loc[g].x3,loc[g].x4,loc[g].y1, 
    88             loc[g].y2,loc[g].y3,loc[g].y4,loc[g].z1,loc[g].z2,loc[g].z3,loc[g].z4) 
    89  
    90     locate+="object3D.add(object%s);\n"%numeration     
    91     return locate 
    92      
    93         
    94 def function_generate_menu(numeration,name): 
    95  
    96     onclick="change_part"+str(numeration)+"(\\\"click\\\")"  
    97     return "<li > <a href='#' id='li-part-%s' onClick='%s'><b onClick='%s'></b>%s </a>" % (numeration, onclick,onclick,name) 
    98      
    99      
    100           
    101 function_head = """ 
    102 function change_part%(numeration)s(attr) { 
     4    _header = """var object3D = new THREE.Object3D(); 
     5var part_to_object = {}; 
     6var part_to_parts = {}; 
     7var part0 = new THREE.Object3D(); 
     8""" 
     9    _function_head = """ 
     10function change_part%(counter)s(attr) { 
    10311    if (attr == "click"){ 
    104      
    105         part%(numeration)s.visible=!part%(numeration)s.visible; 
    106          
     12        part%(counter)s.visible=!part%(counter)s.visible; 
    10713    } 
    10814    else{ 
    109         part%(numeration)s.visible=attr; 
     15        part%(counter)s.visible=attr; 
    11016    }        
    11117"""  
    11218 
     19    _function_change_part = """ 
     20    change_part%(child_counter)s(part%(counter)s.visible);  
     21    """ 
     22    _function_change_object = """ 
     23    object%(object_counter)s.visible=part%(counter)s.visible;    
     24    """                                     
     25  
     26    _menu_tpl = """ 
     27function menu() { 
     28    element = document.createElement("div"); 
     29    element.id="root"; 
     30    element.innerHTML ="%s</li></ul>"; 
     31    document.getElementById("menu_").appendChild(element); 
     32} 
     33""" 
     34    def __init__(self, product): 
     35        self.product = product 
     36        self.menu_items = [] 
     37        self.js = [] 
     38        self.counter = 0 
     39        self.locations = [] 
    11340 
    114 function_change_part = """ 
    115     change_part%(part_numeration_child)s(part%(numeration)s.visible);  
    116 """ 
    117 function_change_object = """ 
    118     object%(object_numeration)s.visible=part%(numeration)s.visible;    
    119 """                                     
     41    def get_js(self): 
     42        if not self.product: 
     43            return "" 
     44        self.menu_items.append(self._get_menu_item(0, self.product.name)) 
     45        self._process(self.product, [], self.counter) 
     46        return self._header + self._get_menu() + "".join(self.js) 
     47     
     48    def _process(self, product, locations, old_counter): 
     49        self.counter += 1 
     50        self.menu_items.append("<ul>") 
     51        if product.geometry: 
     52            self._add_object3d(old_counter, self.counter, product, locations) 
     53        else: 
     54            generated_parts = [] 
     55            for link in product.links: 
     56                for i in range(link.quantity): 
     57                    loc2 = locations[:] 
     58                    loc2.append(link.locations[i]) 
     59                    generated_parts.append(self.counter) 
     60                    self.menu_items.append(self._get_menu_item(self.counter, link.names[i])) 
     61                    self._process(link.product, loc2, self.counter) 
     62                    self.menu_items.append("</li>") 
     63            self._add_parts(old_counter, generated_parts) 
     64        self.menu_items.append("</ul>") 
    12065 
     66    def _get_menu(self): 
     67        return self._menu_tpl % ("".join(self.menu_items)) 
     68 
     69    def _add_parts(self, counter, generated_parts): 
     70        parts_definition = [] 
     71        function = [self._function_head % locals()] 
     72        part_to_parts = ['part_to_parts["part%s"] = [' % counter] 
     73        for child_counter in generated_parts: 
     74            parts_definition.append("var part%s=new THREE.Object3D();\n" % child_counter) 
     75            function.append(self._function_change_part % locals()) 
     76            part_to_parts.append('"part%s", ' % child_counter) 
     77        part_to_parts.append("];\n") 
     78        function.append("}\n") 
     79        self.js.extend(parts_definition) 
     80        self.js.extend(function) 
     81        self.js.extend(part_to_parts) 
     82      
     83    def _add_object3d(self, counter, object_counter, product, loc): 
     84        reference = product.geometry 
     85        part_id = str(product.doc_id)         
     86        js = self.js                      
     87        function = ((self._function_head + self._function_change_object  
     88                + "}\npart_to_object['part%(counter)s'] = object%(object_counter)s;") % locals()) 
     89 
     90        counter = object_counter 
     91        js.append("var object%s=new THREE.Mesh(_%s_%s,material_for_%s_%s );\n"%(counter, reference, 
     92            part_id, reference, part_id)) 
     93        js.append("object%s.matrixAutoUpdate = false;\n" % counter) 
     94        for l in loc: 
     95            js.append(""" 
     96object%s.matrix.multiplySelf(new THREE.Matrix4(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,0,0,0,1));\n""" 
     97                % (counter, l.x1, l.x2, l.x3, l.x4, 
     98                            l.y1, l.y2, l.y3, l.y4, 
     99                            l.z1, l.z2, l.z3, l.z4)) 
     100        js.append("object3D.add(object%s);\n" % counter) 
     101        js.append(function)         
     102            
     103    def _get_menu_item(self, counter,name): 
     104 
     105        onclick="change_part"+str(counter)+"(\\\"click\\\")"  
     106        return "<li > <a href='#' id='li-part-%s' onClick='%s'><b onClick='%s'></b>%s </a>" % (counter, onclick,onclick,name) 
     107              
     108 
  • trunk/openPLM/apps/document3D/models.py

    r1441 r1447  
    299299        else: 
    300300            gfs = GeometryFile.objects.filter(stp=doc_file) 
    301  
    302301        return gfs.values_list("file", flat=True) 
    303302 
     
    343342    :param doc_file: :class:`.DocumentFile` 
    344343    """ 
    345     to_delete=GeometryFile.objects.filter(stp=doc_file) 
    346     list_files=list(to_delete.values_list("file", flat=True)) 
    347     delete_files(list_files,media3DGeometryFile.location) 
     344    to_delete = GeometryFile.objects.filter(stp=doc_file) 
     345    files = to_delete.values_list("file", flat=True) 
     346    delete_files(files, media3DGeometryFile.location) 
    348347    to_delete.delete() 
    349  
    350348 
    351349 
     
    386384    """ 
    387385 
    388     to_delete=ArbreFile.objects.filter(stp=doc_file) 
    389     list_files=list(to_delete.values_list("file", flat=True)) 
    390     delete_files(list_files,media3DArbreFile.location) 
     386    to_delete = ArbreFile.objects.filter(stp=doc_file) 
     387    files = to_delete.values_list("file", flat=True) 
     388    delete_files(files, media3DArbreFile.location) 
    391389    to_delete.delete() 
    392390 
    393 def delete_files(list_files,location=""): 
    394     for name in list_files: 
     391def delete_files(files, location=""): 
     392    for name in files: 
    395393        filename = os.path.join(location, name) 
    396394        if os.path.exists(filename) and os.path.isfile(filename): 
  • trunk/openPLM/apps/document3D/views.py

    r1439 r1447  
    22from openPLM.apps.document3D.forms import * 
    33from openPLM.apps.document3D.models import * 
    4 from openPLM.apps.document3D.arborescense import * 
     4from openPLM.apps.document3D.arborescense import JSGenerator 
    55from openPLM.apps.document3D.classes import * 
    66from openPLM.plmapp.forms import * 
     
    4949        product=ArbreFile_to_Product(doc_file,recursif=True) 
    5050        GeometryFiles= obj.get_all_geometry_files(doc_file) 
    51         javascript_arborescense=generate_javascript_for_3D(product) 
     51        javascript_arborescense = JSGenerator(product).get_js() 
    5252 
    5353    ctx.update({ 
Note: See TracChangeset for help on using the changeset viewer.