/[ascend]/branches/mike/pygtk/canvas/blockcanvas.py
ViewVC logotype

Contents of /branches/mike/pygtk/canvas/blockcanvas.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3160 - (show annotations) (download) (as text)
Sun Jul 31 12:30:52 2016 UTC (2 years, 2 months ago) by mike
File MIME type: text/x-python
File size: 4257 byte(s)
Solve svg files issue, modify piping system, add comments

1 import re
2
3 from gaphas import Canvas
4
5 from blockitem import DefaultBlockItem, GraphicalBlockItem
6 from blockline import BlockLine
7
8
9 UNITS_RE = re.compile("([-+]?(\d+(\.\d*)?|\d*\.d+)([eE][-+]?\d+)?)\s*(.*)");
10
11 saved_model = None
12
13 class BlockCanvas(Canvas):
14
15 def __init__(self):
16 super(BlockCanvas, self).__init__()
17 self.filestate = 0
18 self.canvasmodelstate = 'Unsolved'
19 self.model_library = ''
20 self.saved_data = None
21 self.filename = None
22 self.user_code = ''
23
24 def update_constraints(self, items):
25 """
26 Update constraints. Also variables may be marked as dirty before the
27 constraint solver kicks in.
28 """
29 # request solving of external constraints associated with dirty items
30 request_resolve = self._solver.request_resolve
31 for item in items:
32 if hasattr(item,'ports'):
33 for p in item._ports:
34 if hasattr(p,"point"):
35 request_resolve(p.point.x)
36 request_resolve(p.point.y)
37
38 super(BlockCanvas,self).update_constraints(items)
39
40 def reattach_ascend(self, ascwrap, notesdb):
41 """
42 After unpickling a canvas, this method gives a way of reattaching
43 the model to an ASCEND library, by connecting the types named in the
44 pickle with the typenames present in the Library.
45 """
46 # FIXME need to set ASCEND to *load* the missing types if they're not
47 # already in the Library.
48 items = []
49 for item in self.get_all_items():
50 if not hasattr(item, 'blockinstance'):
51 continue
52 bi = item.blockinstance
53 bi.reattach_ascend(ascwrap,notesdb)
54
55 #if bi.blocktype.type is None:
56 # bi.blocktype.reattach_ascend(ascwrap, notesdb)
57 '''
58 def set_stream(self,stream):
59 items = self.get_all_items()
60 for item in items:
61 if type(item)==DefaultBlockItem:
62 bi = item.blockinstance
63 bi.stream=stream
64 '''
65
66 def __str__(self):
67 """
68 Create ASCEND code corresponding to canvas model, return the
69 code as a string.
70 Note: This function uses Python's advanced string formatting capability.
71 """
72
73 # FIXME it should be possible to perform this function at an
74 # application layer, without using routines from Gaphas
75
76 string = '''
77 (* automatically generated model*)
78 REQUIRE "{lib_name}";
79
80 MODEL canvasmodel;
81 {is_a}
82 {streams}
83 {are_the_same}
84 {canvas_user_code}
85 METHODS
86 METHOD canvas_user_code;
87 {canvas_user_code}
88 END canvas_user_code;
89 METHOD parameter_code;
90 {parameter_code}
91 END parameter_code;
92 METHOD on_load;
93 RUN canvas_user_code;
94 RUN parameter_code;
95 END on_load;
96 END canvasmodel;
97 '''
98 replacement_fields = {'lib_name':str(self.model_library),'is_a':'','are_the_same':'',
99 'canvas_user_code':'','parameter_code':'','block_user_code':''
100 ,'streams':''}
101
102 items = self.get_all_items()
103
104 def parse(item):
105 if type(item)==DefaultBlockItem or type(item)==GraphicalBlockItem:
106 bi = item.blockinstance
107 replacement_fields['is_a']+=str(bi)
108 specify = filter(lambda param:bi.params[param].value != None,bi.params)
109 fix = filter(lambda param:bi.params[param].fix == True,bi.params)
110 specify = filter(lambda x:not (x in fix),specify)
111 specify = map(lambda param:'\t{0}.{1}:={2};\n'.
112 format(bi.name,param,bi.params[param].value),specify)
113 fix = map(lambda param:'\tFIX {0}.{1};\n\t{0}.{1}:={2}{4}{3}{5};\n'.
114 format(bi.name,param,bi.params[param].value,
115 bi.params[param].units,'{','}'),fix)
116 try:
117 replacement_fields['parameter_code']+=\
118 reduce(lambda x,y:x+y,specify)
119 except TypeError:
120 pass
121 try:
122 replacement_fields['parameter_code']+=\
123 reduce(lambda x,y:x+y,fix)
124 except TypeError:
125 pass
126
127 if type(item)==BlockLine:
128 replacement_fields['are_the_same']+=str(item.lineinstance)
129
130 map(parse,items)
131
132 replacement_fields['canvas_user_code'] = self.user_code
133 return string.format(**replacement_fields)
134
135 def __getstate__(self):
136 """
137 Placeholder for any special pickling stuff that we want to do with
138 our canvas.
139 """
140 return super(BlockCanvas,self).__getstate__()
141
142 def __setstate__(self, state):
143 """
144 Placeholder for any special pickling stuff that we want to do with
145 our canvas.
146 """
147 super(BlockCanvas,self).__setstate__(state)
148
149 # vim: set ts=4 noet:

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22