source: main/branches/3D/openPLM/document3D/STP_converter_JSS.py @ 597

Revision 597, 7.9 KB checked in by agalech, 9 years ago (diff)

branch 3D: 3D view: make it possible to show/hide each shape of a stp file

Line 
1##Copyright 2008-2011 Thomas Paviot (tpaviot@gmail.com)
2##
3##This file is part of pythonOCC.
4##
5##pythonOCC is free software: you can redistribute it and/or modify
6##it under the terms of the GNU Lesser General Public License as published by
7##the Free Software Foundation, either version 3 of the License, or
8##(at your option) any later version.
9##
10##pythonOCC is distributed in the hope that it will be useful,
11##but WITHOUT ANY WARRANTY; without even the implied warranty of
12##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13##GNU Lesser General Public License for more details.
14##
15##You should have received a copy of the GNU Lesser General Public License
16##along with pythonOCC.  If not, see <http://www.gnu.org/licenses/>.
17import os, os.path
18from openPLM.document3D.models import *
19from OCC.Quantity import *
20from OCC.TDataStd import *
21from OCC.TopoDS import *
22from OCC.BRep import *
23from OCC.STEPControl import *
24from OCC.Interface import *
25
26from OCC.IFSelect import *
27from OCC.TColStd import *
28
29from OCC.XCAFApp import *
30from OCC.STEPCAFControl import *
31from OCC.TDocStd import *
32from OCC.TCollection import *
33from OCC.XCAFDoc import *
34from OCC.TDF import *
35#from OCC.TDataStd import *
36from OCC import XCAFApp, TDocStd, TCollection, XCAFDoc, BRepPrimAPI, Quantity, TopLoc, gp, TPrsStd, XCAFPrs
37
38
39
40##Puede haver dos root?
41## y si el shapeTOPO no es ABS 1 o 2
42##SI tengo una location dentro de otra location en un shape referenciado?
43
44
45class NEW_STEP_Import(object):
46
47    def __init__(self, filename, layer_name='layer-00'):
48        self.filename = filename
49        self._shapes = [] #an empty string
50        self._shapes_avec_nom= []
51        self._vertices= []
52        self._faces= []
53       
54    def get_shapes(self):
55        return self._shapes 
56    def get_shapes_avec_nom(self):
57        return self._shapes_avec_nom
58       
59    def read_file(self):     
60        STEPReader = STEPCAFControl_Reader()
61        STEPReader.ReadFile(self.filename) 
62        #STEPReader.ReadFile("/home/linux/test/s1/1.stp")
63        h_doc = TDocStd.Handle_TDocStd_Document()
64        app = XCAFApp.GetApplication().GetObject()
65        app.NewDocument(TCollection.TCollection_ExtendedString("MDTV-CAF"),h_doc)
66
67
68        STEPReader.Transfer(h_doc)
69
70        doc = h_doc.GetObject()
71        h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool_ShapeTool(doc.Main())
72        l_Colors = XCAFDoc.XCAFDoc_DocumentTool_ColorTool(doc.Main())
73       
74        ColorLabels = TDF_LabelSequence()
75        l_Labels_root = TDF_LabelSequence()
76        l_LabelShapes = TDF_LabelSequence()
77        l_Colors.GetObject().GetColors(ColorLabels)
78        color_tool=l_Colors.GetObject()
79        print 'Number of colors=%i'%ColorLabels.Length()
80
81        l_ComponentShapes = TDF_LabelSequence()
82        shape_tool = h_shape_tool.GetObject()
83        shape_tool.GetShapes(l_LabelShapes)
84        shape_tool.GetFreeShapes(l_Labels_root)
85       
86
87                   
88        #for i in range(l_Labels_root.Length()):
89        for i in range(l_Labels_root.Length()):
90            loc=[]
91            self._shapes_avec_nom.append(arbre_nom_shapes(Nom_Label_Shape(l_Labels_root.Value(i+1))))
92            parcours_recursif(l_Labels_root.Value(i+1),shape_tool,self._shapes_avec_nom[i].children,color_tool,loc)
93
94           
95        return True             
96       
97
98def parcours_recursif(label,shape_tool,arbre_shape,color_tool,loc):
99
100    l_ComponentShapes = TDF_LabelSequence()
101    colour_chercher(label,color_tool)
102   
103   
104   
105    if(shape_tool.IsReference(label)):   
106        lab=TDF_Label()
107        shape_tool.GetReferredShape(label,lab)
108        shape_tool.GetComponents(lab,l_ComponentShapes)
109        if(l_ComponentShapes.Length()>0):
110            loc.append(shape_tool.GetLocation(label))
111            for f in range(l_ComponentShapes.Length()):
112                arbre_shape.append(arbre_nom_shapes(Nom_Label_Shape(l_ComponentShapes.Value(f+1))))
113                 
114                parcours_recursif(l_ComponentShapes.Value(f+1),shape_tool,arbre_shape[f].children,color_tool,loc[:])
115        else:
116            compShape=shape_tool.GetShape(label)
117            for i in range(len(loc)):
118                compShape.Move(loc[len(loc)-i-1])       
119            arbre_shape.append(compShape)       
120    else:
121        shape_tool.GetComponents(label,l_ComponentShapes)
122        if(l_ComponentShapes.Length()>0):     
123            for f in range(l_ComponentShapes.Length()):           
124                arbre_shape.append(arbre_nom_shapes(Nom_Label_Shape(l_ComponentShapes.Value(f+1))))   
125                parcours_recursif(l_ComponentShapes.Value(f+1),shape_tool,arbre_shape[f].children,color_tool,loc[:])     
126        else:
127               
128            compShape=shape_tool.GetShape(label)
129            if loc:
130                print "y aqui no entra"
131                compShape.Move(loc)       
132            arbre_shape.append(compShape)             
133
134
135def colour_chercher(label,color_tool):
136    c=Quantity_Color()
137    if( color_tool.GetColor(label,XCAFDoc_ColorSurf,c) or  color_tool.GetColor(label,XCAFDoc_ColorCurv,c) or color_tool.GetColor(label,XCAFDoc_ColorGen,c)):
138        print "ALELUY##########################" 
139        print ""
140        print "Color encontrado para : " , Nom_Label_Shape(label)
141        print ""     
142        print "################################"
143    else:
144        print "No hay color para : " , Nom_Label_Shape(label)     
145   
146           
147def Nom_Label_Shape(lab):
148
149            entry = TCollection.TCollection_AsciiString()
150            TDF_Tool.Entry(lab,entry)
151            N = Handle_TDataStd_Name()
152            lab.FindAttribute(TDataStd_Name_GetID(),N)
153            n=N.GetObject()
154            return n.Get().PrintToString()
155       
156       
157"""                   
158def parcours_recursif(label,shape_tool,arbre_shape,loc=None):
159
160    l_ComponentShapes = TDF_LabelSequence()           
161    shape_tool.GetComponents(label,l_ComponentShapes)
162    if(l_ComponentShapes.Length()>0):
163       
164        for f in range(l_ComponentShapes.Length()):
165            #shape.append([])           
166            arbre_shape.append(arbre_nom_shapes(Nom_Label_Shape(l_ComponentShapes.Value(f+1))))   
167            parcours_recursif(l_ComponentShapes.Value(f+1),shape_tool,arbre_shape[f].children,loc)     
168    else:
169
170            compShape=shape_tool.GetShape(label)       
171            arbre_shape.append(compShape)   
172"""       
173       
174"""
175parcours_recursif(l_Labels_root.Value(i+1),shape_tool,self._shapes_avec_nom[i].children)
176def parcours_recursif(label,shape_tool,arbre_shape,loc=None):
177
178    l_ComponentShapes = TDF_LabelSequence()
179   
180    if(shape_tool.IsReference(label)):   
181        lab=TDF_Label()
182        shape_tool.GetReferredShape(label,lab)
183        shape_tool.GetComponents(lab,l_ComponentShapes)
184        if not loc:
185            print "entra en loc para " , Nom_Label_Shape(label)
186            loc=shape_tool.GetLocation(label) 
187        if(l_ComponentShapes.Length()>0):
188            for f in range(l_ComponentShapes.Length()):
189                arbre_shape.append(arbre_nom_shapes(Nom_Label_Shape(l_ComponentShapes.Value(f+1))))   
190                parcours_recursif(l_ComponentShapes.Value(f+1),shape_tool,arbre_shape[f].children,loc)
191        else:
192            compShape=shape_tool.GetShape(label)
193            if loc:
194                compShape.Move(loc)       
195            arbre_shape.append(compShape)       
196    else:
197        shape_tool.GetComponents(label,l_ComponentShapes)
198        if(l_ComponentShapes.Length()>0):     
199            for f in range(l_ComponentShapes.Length()):           
200                arbre_shape.append(arbre_nom_shapes(Nom_Label_Shape(l_ComponentShapes.Value(f+1))))   
201                parcours_recursif(l_ComponentShapes.Value(f+1),shape_tool,arbre_shape[f].children,loc)     
202        else:
203               
204            compShape=shape_tool.GetShape(label)
205            if loc:
206                print "y aqui no entra"
207                compShape.Move(loc)       
208            arbre_shape.append(compShape)   
209"""               
210
211
212
Note: See TracBrowser for help on using the repository browser.