STEP parser writes GraphViz files
This commit is contained in:
parent
7d06fc0136
commit
50492a5f5f
|
@ -23,7 +23,7 @@
|
||||||
# ARE DISCLAIMED.
|
# ARE DISCLAIMED.
|
||||||
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||||
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
@ -54,7 +54,7 @@ class Model:
|
||||||
# each time an instance is added to the model, count is incremented
|
# each time an instance is added to the model, count is incremented
|
||||||
self._instances = {}
|
self._instances = {}
|
||||||
self._number_of_instances = 0
|
self._number_of_instances = 0
|
||||||
|
|
||||||
def add_instance(self, instance):
|
def add_instance(self, instance):
|
||||||
'''
|
'''
|
||||||
Adds an instance to the model
|
Adds an instance to the model
|
||||||
|
@ -88,7 +88,7 @@ class Part21EntityInstance:
|
||||||
self._attributes_definition = attributes
|
self._attributes_definition = attributes
|
||||||
print self._entity_name
|
print self._entity_name
|
||||||
print self._attributes_definition
|
print self._attributes_definition
|
||||||
|
|
||||||
|
|
||||||
class Part21Parser:
|
class Part21Parser:
|
||||||
"""
|
"""
|
||||||
|
@ -114,11 +114,11 @@ class Part21Parser:
|
||||||
for item in self._number_of_ancestors.keys():
|
for item in self._number_of_ancestors.keys():
|
||||||
if len(self._number_of_ancestors[item])==0:
|
if len(self._number_of_ancestors[item])==0:
|
||||||
del self._number_of_ancestors[item]
|
del self._number_of_ancestors[item]
|
||||||
|
|
||||||
def get_schema_name(self):
|
def get_schema_name(self):
|
||||||
return self._schema_name
|
return self._schema_name
|
||||||
print schema_name
|
print schema_name
|
||||||
|
|
||||||
def get_number_of_instances(self):
|
def get_number_of_instances(self):
|
||||||
return len(self._instances_definition.keys())
|
return len(self._instances_definition.keys())
|
||||||
|
|
||||||
|
@ -132,8 +132,8 @@ class Part21Parser:
|
||||||
break
|
break
|
||||||
# there may be a multline definition. In this case, we read lines untill we found
|
# there may be a multline definition. In this case, we read lines untill we found
|
||||||
# a ;
|
# a ;
|
||||||
#while (not line.endswith(";\r\n")): #its a multiline
|
while (line.find(';') == -1): #its a multiline
|
||||||
# line = line.replace("\r\n","") + fp.readline()
|
line = line.replace("\n","").replace("\r","") + fp.readline()
|
||||||
# parse line
|
# parse line
|
||||||
match_instance_definition = INSTANCE_DEFINITION_RE.search(line) # id,name,attrs
|
match_instance_definition = INSTANCE_DEFINITION_RE.search(line) # id,name,attrs
|
||||||
if match_instance_definition:
|
if match_instance_definition:
|
||||||
|
@ -179,14 +179,14 @@ class Part21Population(object):
|
||||||
self._aggregate_scope = []
|
self._aggregate_scope = []
|
||||||
self._aggr_scope = False
|
self._aggr_scope = False
|
||||||
self.create_entity_instances()
|
self.create_entity_instances()
|
||||||
|
|
||||||
def create_entity_instances(self):
|
def create_entity_instances(self):
|
||||||
""" Starts entity instances creation
|
""" Starts entity instances creation
|
||||||
"""
|
"""
|
||||||
for number_of_ancestor in self._part21_loader._number_of_ancestors.keys():
|
for number_of_ancestor in self._part21_loader._number_of_ancestors.keys():
|
||||||
for entity_definition_id in self._part21_loader._number_of_ancestors[number_of_ancestor]:
|
for entity_definition_id in self._part21_loader._number_of_ancestors[number_of_ancestor]:
|
||||||
self.create_entity_instance(entity_definition_id)
|
self.create_entity_instance(entity_definition_id)
|
||||||
|
|
||||||
def create_entity_instance(self, instance_id):
|
def create_entity_instance(self, instance_id):
|
||||||
instance_definition = self._part21_loader._instances_definition[instance_id]
|
instance_definition = self._part21_loader._instances_definition[instance_id]
|
||||||
print "Instance definition to process",instance_definition
|
print "Instance definition to process",instance_definition
|
||||||
|
@ -199,7 +199,7 @@ class Part21Population(object):
|
||||||
instance_attributes = instance_definition[1]
|
instance_attributes = instance_definition[1]
|
||||||
print "instance_attributes:",instance_attributes
|
print "instance_attributes:",instance_attributes
|
||||||
a = object_(*instance_attributes)
|
a = object_(*instance_attributes)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
# Copyright (c) 2014, Juergen Riegel (FreeCAD@juergen-riegel.net)
|
# Copyright (c) 2014, Juergen Riegel (FreeCAD@juergen-riegel.net)
|
||||||
# Copyright (c) 2011, Thomas Paviot (tpaviot@gmail.com)
|
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
|
|
||||||
# This file is part of the StepClassLibrary (SCL).
|
# This file is part of the StepClassLibrary (SCL).
|
||||||
|
@ -42,8 +41,7 @@ import Part21,sys
|
||||||
|
|
||||||
|
|
||||||
__title__="Simple Part21 STEP reader"
|
__title__="Simple Part21 STEP reader"
|
||||||
__author__ = "Juergen Riegel, Thomas Paviot"
|
__author__ = "Juergen Riegel"
|
||||||
__url__ = "http://www.freecadweb.org"
|
|
||||||
__version__ = "0.1 (Jan 2014)"
|
__version__ = "0.1 (Jan 2014)"
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,11 +58,35 @@ class SimpleParser:
|
||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
self._p21loader = Part21.Part21Parser("gasket1.p21")
|
self._p21loader = Part21.Part21Parser("gasket1.p21")
|
||||||
|
self._p21loader._number_of_ancestors = {} # not needed, save memory
|
||||||
self.schemaModule = None
|
self.schemaModule = None
|
||||||
self.schemaClasses = None
|
self.schemaClasses = None
|
||||||
self.instanceMape = {}
|
self.instanceMape = {}
|
||||||
|
#for i in self._p21loader._instances_definition.keys():
|
||||||
|
# print i,self._p21loader._instances_definition[i][0],self._p21loader._instances_definition[i][1]
|
||||||
|
|
||||||
|
def _writeGraphVizEdge(self,num,attrList,file):
|
||||||
|
for i in attrList:
|
||||||
|
if isinstance(i,list):
|
||||||
|
self._writeGraphVizEdge(num,i,file)
|
||||||
|
elif isinstance(i,str):
|
||||||
|
if not i == '' and i[0] == '#':
|
||||||
|
key = int(i[1:])
|
||||||
|
file.write(' '+`num`+' -> '+`key`+'\n')
|
||||||
|
|
||||||
|
|
||||||
|
def writeGraphViz(self,fileName):
|
||||||
|
print "Writing GraphViz file %s..."%fileName,
|
||||||
|
gvFile = open(fileName,'w')
|
||||||
|
|
||||||
|
gvFile.write('digraph G {\n node [fontname=Verdana,fontsize=12]\n node [style=filled]\n node [fillcolor="#EEEEEE"]\n node [color="#EEEEEE"]\n edge [color="#31CEF0"]\n')
|
||||||
for i in self._p21loader._instances_definition.keys():
|
for i in self._p21loader._instances_definition.keys():
|
||||||
print i,self._p21loader._instances_definition[i][0],self._p21loader._instances_definition[i][1]
|
entityStr = '#'+`i`
|
||||||
|
nameStr = self._p21loader._instances_definition[i][0].lower()
|
||||||
|
sttrStr = `self._p21loader._instances_definition[i][1]`.replace('"','').replace("'",'')
|
||||||
|
gvFile.write(' '+`i`+' [label="'+entityStr+'\n'+nameStr+'\n'+sttrStr+'"]\n')
|
||||||
|
self._writeGraphVizEdge( i,self._p21loader._instances_definition[i][1],gvFile)
|
||||||
|
gvFile.write('}\n')
|
||||||
|
|
||||||
def instaciate(self):
|
def instaciate(self):
|
||||||
"""Instaciate the python classe from the enteties"""
|
"""Instaciate the python classe from the enteties"""
|
||||||
|
@ -84,15 +106,11 @@ class SimpleParser:
|
||||||
#print i
|
#print i
|
||||||
if not self.instanceMape.has_key(i):
|
if not self.instanceMape.has_key(i):
|
||||||
self._create_entity_instance(i)
|
self._create_entity_instance(i)
|
||||||
#for number_of_ancestor in self._p21loader._number_of_ancestors.keys():
|
|
||||||
# for entity_definition_id in self._p21loader._number_of_ancestors[number_of_ancestor]:
|
|
||||||
# print entity_definition_id,':',self._p21loader._instances_definition[entity_definition_id]
|
|
||||||
# self._create_entity_instance(entity_definition_id)
|
|
||||||
|
|
||||||
def _create_entity_instance(self, instance_id):
|
def _create_entity_instance(self, instance_id):
|
||||||
if self._p21loader._instances_definition.has_key(instance_id):
|
if self._p21loader._instances_definition.has_key(instance_id):
|
||||||
instance_definition = self._p21loader._instances_definition[instance_id]
|
instance_definition = self._p21loader._instances_definition[instance_id]
|
||||||
print "Instance definition to process",instance_definition
|
#print "Instance definition to process",instance_definition
|
||||||
# first find class name
|
# first find class name
|
||||||
class_name = instance_definition[0].lower()
|
class_name = instance_definition[0].lower()
|
||||||
#print "Class name:%s"%class_name
|
#print "Class name:%s"%class_name
|
||||||
|
@ -105,7 +123,7 @@ class SimpleParser:
|
||||||
self._transformAttributes(instance_attributes)
|
self._transformAttributes(instance_attributes)
|
||||||
print 'Attribute list after transform: ',instance_attributes
|
print 'Attribute list after transform: ',instance_attributes
|
||||||
|
|
||||||
self.instanceMape[instance_id] = int(41) # dummy instance to test
|
self.instanceMape[instance_id] = str('dummy#:'+str(instance_id)) # dummy instance to test
|
||||||
else:
|
else:
|
||||||
print '############################# lost entity: ',instance_id
|
print '############################# lost entity: ',instance_id
|
||||||
self.instanceMape[instance_id] = int(41) # dummy
|
self.instanceMape[instance_id] = int(41) # dummy
|
||||||
|
@ -122,15 +140,18 @@ class SimpleParser:
|
||||||
print 'empty string'
|
print 'empty string'
|
||||||
elif i[0] == '#':
|
elif i[0] == '#':
|
||||||
key = int(i[1:])
|
key = int(i[1:])
|
||||||
print 'Item: ',int(i[1:])
|
#print 'Item: ',int(i[1:])
|
||||||
if self.instanceMape.has_key(key):
|
if self.instanceMape.has_key(key):
|
||||||
attrList[n] = self.instanceMape[key]
|
attrList[n] = self.instanceMape[key]
|
||||||
else:
|
else:
|
||||||
self._create_entity_instance(key)
|
self._create_entity_instance(key)
|
||||||
if not self.instanceMape.has_key(key):
|
if not self.instanceMape.has_key(key):
|
||||||
raise NameError("Needed instance not instanciated: ",key)
|
raise NameError("Needed instance not instanciated: ",key)
|
||||||
|
else:
|
||||||
|
attrList[n] = self.instanceMape[key]
|
||||||
elif i[0] == '$':
|
elif i[0] == '$':
|
||||||
print 'Dollar'
|
#print 'Dollar'
|
||||||
|
pass
|
||||||
elif i[0] == "'":
|
elif i[0] == "'":
|
||||||
print 'Dopelstring: ',i[1:-1]
|
print 'Dopelstring: ',i[1:-1]
|
||||||
else:
|
else:
|
||||||
|
@ -142,4 +163,5 @@ class SimpleParser:
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.path.append('..') # path where config_control_design.py is found
|
sys.path.append('..') # path where config_control_design.py is found
|
||||||
parser = SimpleParser("gasket1.p21") # simple test file
|
parser = SimpleParser("gasket1.p21") # simple test file
|
||||||
parser.instaciate()
|
#parser.instaciate()
|
||||||
|
parser.writeGraphViz('TestGrap.gv')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user