Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
STEP parser writes GraphViz files
  • Loading branch information
jriegel committed Jan 7, 2014
1 parent 7d06fc0 commit 50492a5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
20 changes: 10 additions & 10 deletions src/Mod/Import/App/SCL/Part21.py
Expand Up @@ -23,7 +23,7 @@
# ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
# 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
# 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
Expand Down Expand Up @@ -54,7 +54,7 @@ def __init__(self,name):
# each time an instance is added to the model, count is incremented
self._instances = {}
self._number_of_instances = 0

def add_instance(self, instance):
'''
Adds an instance to the model
Expand Down Expand Up @@ -88,7 +88,7 @@ def __init__(self,entity_name,attributes):
self._attributes_definition = attributes
print self._entity_name
print self._attributes_definition


class Part21Parser:
"""
Expand All @@ -114,11 +114,11 @@ def __init__(self, filename):
for item in self._number_of_ancestors.keys():
if len(self._number_of_ancestors[item])==0:
del self._number_of_ancestors[item]

def get_schema_name(self):
return self._schema_name
print schema_name

def get_number_of_instances(self):
return len(self._instances_definition.keys())

Expand All @@ -132,8 +132,8 @@ def parse_file(self):
break
# there may be a multline definition. In this case, we read lines untill we found
# a ;
#while (not line.endswith(";\r\n")): #its a multiline
# line = line.replace("\r\n","") + fp.readline()
while (line.find(';') == -1): #its a multiline
line = line.replace("\n","").replace("\r","") + fp.readline()
# parse line
match_instance_definition = INSTANCE_DEFINITION_RE.search(line) # id,name,attrs
if match_instance_definition:
Expand Down Expand Up @@ -179,14 +179,14 @@ def __init__(self, part21_loader):
self._aggregate_scope = []
self._aggr_scope = False
self.create_entity_instances()

def create_entity_instances(self):
""" Starts entity instances creation
"""
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]:
self.create_entity_instance(entity_definition_id)

def create_entity_instance(self, instance_id):
instance_definition = self._part21_loader._instances_definition[instance_id]
print "Instance definition to process",instance_definition
Expand All @@ -199,7 +199,7 @@ def create_entity_instance(self, instance_id):
instance_attributes = instance_definition[1]
print "instance_attributes:",instance_attributes
a = object_(*instance_attributes)

if __name__ == "__main__":
import time
import sys
Expand Down
48 changes: 35 additions & 13 deletions src/Mod/Import/App/SCL/SimpleReader.py
@@ -1,5 +1,4 @@
# Copyright (c) 2014, Juergen Riegel (FreeCAD@juergen-riegel.net)
# Copyright (c) 2011, Thomas Paviot (tpaviot@gmail.com)
# All rights reserved.

# This file is part of the StepClassLibrary (SCL).
Expand Down Expand Up @@ -42,8 +41,7 @@


__title__="Simple Part21 STEP reader"
__author__ = "Juergen Riegel, Thomas Paviot"
__url__ = "http://www.freecadweb.org"
__author__ = "Juergen Riegel"
__version__ = "0.1 (Jan 2014)"


Expand All @@ -60,11 +58,35 @@ def __init__(self, filename):
import time
import sys
self._p21loader = Part21.Part21Parser("gasket1.p21")
self._p21loader._number_of_ancestors = {} # not needed, save memory
self.schemaModule = None
self.schemaClasses = None
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():
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):
"""Instaciate the python classe from the enteties"""
Expand All @@ -84,15 +106,11 @@ def instaciate(self):
#print i
if not self.instanceMape.has_key(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):
if self._p21loader._instances_definition.has_key(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
class_name = instance_definition[0].lower()
#print "Class name:%s"%class_name
Expand All @@ -105,7 +123,7 @@ def _create_entity_instance(self, instance_id):
self._transformAttributes(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:
print '############################# lost entity: ',instance_id
self.instanceMape[instance_id] = int(41) # dummy
Expand All @@ -122,15 +140,18 @@ def _transformAttributes(self,attrList):
print 'empty string'
elif i[0] == '#':
key = int(i[1:])
print 'Item: ',int(i[1:])
#print 'Item: ',int(i[1:])
if self.instanceMape.has_key(key):
attrList[n] = self.instanceMape[key]
else:
self._create_entity_instance(key)
if not self.instanceMape.has_key(key):
raise NameError("Needed instance not instanciated: ",key)
else:
attrList[n] = self.instanceMape[key]
elif i[0] == '$':
print 'Dollar'
#print 'Dollar'
pass
elif i[0] == "'":
print 'Dopelstring: ',i[1:-1]
else:
Expand All @@ -142,4 +163,5 @@ def _transformAttributes(self,attrList):
if __name__ == "__main__":
sys.path.append('..') # path where config_control_design.py is found
parser = SimpleParser("gasket1.p21") # simple test file
parser.instaciate()
#parser.instaciate()
parser.writeGraphViz('TestGrap.gv')

0 comments on commit 50492a5

Please sign in to comment.