1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
#
# Copyright (c) 2004, 2005 Art Haas
#
# This file is part of PythonCAD.
#
# PythonCAD is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PythonCAD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PythonCAD; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import sys
from PythonCAD.Generic.image import Image
from PythonCAD.Generic.util import make_region
from PythonCAD.Generic.graphicobject import GraphicObject
from PythonCAD.Generic.color import Color
from PythonCAD.Generic.point import Point
from PythonCAD.Generic.segment import Segment
from PythonCAD.Generic.circle import Circle
from PythonCAD.Generic.arc import Arc
#from PythonCAD.Generic.ellipse import Ellipse
from PythonCAD.Generic.polyline import Polyline
from PythonCAD.Generic.leader import Leader
from PythonCAD.Generic.segjoint import Chamfer, Fillet
from PythonCAD.Generic.conobject import ConstructionObject
from PythonCAD.Generic.text import TextStyle, TextBlock
from PythonCAD.Generic import dimension
from PythonCAD.Generic import util
class Plot(object):
__white = Color(255, 255, 255)
def __init__(self, image):
if not isinstance(image, Image):
raise TypeError, "Invalid image argument: " + `image`
self.__image = image
self.__bounds = None
self.__landscape = False
self.__invert = False
self.__color = False
self.__units = None
self.__data = {}
def __contains__(self, objtype):
if not isinstance(objtype, str):
raise TypeError, "Invalid object type string: " + `objtype`
return objtype in self.__data
def finish(self):
self.__image = self.__bounds = self.__units = None
self.__data.clear()
def setBounds(self, xmin, ymin, xmax, ymax):
self.__bounds = make_region(xmin, ymin, xmax, ymax)
def getBounds(self):
return self.__bounds
def setLandscapeMode(self, flag):
util.test_boolean(flag)
self.__landscape = flag
def getLandscapeMode(self):
return self.__landscape
def invertWhite(self, flag):
util.test_boolean(flag)
self.__invert = flag
def setColorMode(self, flag):
util.test_boolean(flag)
self.__color = flag
def setUnits(self):
self.__units = self.__image.getUnits()
def getUnits(self):
if self.__units is None:
self.setUnits()
return self.__units
def _getGraphicValues(self, obj):
if not isinstance(obj, GraphicObject):
raise TypeError, "Invalid GraphicObject: " + `obj`
_col = None
if self.__color:
_c = obj.getColor()
if self.__invert and _c == Plot.__white:
_col = (0, 0, 0) # black
else:
_col = _c.getColors() # r, g, b
_lt = obj.getLinetype().getList()
_t = obj.getThickness()
return _col, _lt, _t
def getPlotData(self):
if self.__bounds is None:
raise ValueError, "Plot boundary not defined."
if self.__units is None:
self.setUnits()
_xmin, _ymin, _xmax, _ymax = self.__bounds
_image = self.__image
_layers = [_image.getTopLayer()]
while len(_layers):
_layer = _layers.pop()
if _layer.isVisible():
for _obj in _layer.objsInRegion(_xmin, _ymin, _xmax, _ymax):
if isinstance(_obj, ConstructionObject):
continue # do not plot these
if not _obj.isVisible():
continue
if isinstance(_obj, Point):
continue # print points as dots?
elif isinstance(_obj, Segment):
_p1, _p2 = _obj.getEndpoints()
_x1, _y1 = _p1.getCoords()
_x2, _y2 = _p2.getCoords()
_col, _lt, _t = self._getGraphicValues(_obj)
_data = self.__data.setdefault('segments', [])
_data.append((_x1, _y1, _x2, _y2, _col, _lt, _t))
elif isinstance(_obj, Circle):
_x, _y = _obj.getCenter().getCoords()
print "x: %g; y: %g" % (_x, _y)
_r = _obj.getRadius()
print "r: %g" % _r
_col, _lt, _t = self._getGraphicValues(_obj)
_data = self.__data.setdefault('circles', [])
_data.append((_x, _y, _r, _col, _lt, _t))
elif isinstance(_obj, Arc):
_x, _y = _obj.getCenter().getCoords()
_r = _obj.getRadius()
_sa = _obj.getStartAngle()
_ea = _obj.getEndAngle()
_col, _lt, _t = self._getGraphicValues(_obj)
_data = self.__data.setdefault('arcs', [])
_data.append((_x, _y, _r, _sa, _ea, _col, _lt, _t))
elif isinstance(_obj, Leader):
_p1, _p2, _p3 = _obj.getPoints()
_x1, _y1 = _p1.getCoords()
_x2, _y2 = _p2.getCoords()
_x3, _y3 = _p3.getCoords()
_ax1 = _ax2 = _x3
_ay1 = _ay2 = _y3
if _obj.getArrowSize() > 1e-10:
_pts = _obj.getArrowPoints()
_ax1 = _pts[0]
_ay1 = _pts[1]
_ax2 = _pts[2]
_ay2 = _pts[3]
_col, _lt, _t = self._getGraphicValues(_obj)
_data = self.__data.setdefault('leaders', [])
_data.append((_x1, _y1, _x2, _y2, _x3, _y3,
_ax1, _ay1, _ax2, _ay2,
_col, _lt, _t))
elif isinstance(_obj, Polyline):
_pts = []
for _pt in _obj.getPoints():
_pts.append(_pt.getCoords())
_col, _lt, _t = self._getGraphicValues(_obj)
_data = self.__data.setdefault('polylines', [])
_data.append((_pts, _col, _lt, _t))
elif isinstance(_obj, Chamfer):
_p1, _p2 = _obj.getMovingPoints()
_col, _lt, _t = self._getGraphicValues(_obj)
_data = self.__data.setdefault('chamfers', [])
_data.append((_p1.x, _p1.y, _p2.x, _p2.y,
_col, _lt, _t))
elif isinstance(_obj, Fillet):
_x, _y = _obj.getCenter()
_r = _obj.getRadius()
_sa, _ea = _obj.getAngles()
_col, _lt, _t = self._getGraphicValues(_obj)
_data = self.__data.setdefault('fillets', [])
_data.append((_x, _y, _r, _sa, _ea, _col, _lt, _t))
elif isinstance(_obj, dimension.Dimension):
self._saveDimensionData(_obj)
elif isinstance(_obj, TextBlock):
_tbdata = self._getTextBlockData(_obj)
if _tbdata is not None:
_data = self.__data.setdefault('textblocks', [])
_data.append(_tbdata)
else: # fixme
pass
_layers.extend(_layer.getSublayers())
def _getTextBlockData(self, tblock):
_bounds = tblock.getBounds()
if _bounds is None: # raise Exception?
return
_tbdata = {}
_tbdata['bounds'] = _bounds
_tbdata['location'] = tblock.getLocation()
_family = tblock.getFamily()
if _family.find(' ') != -1:
_family = _family.replace(' ', '-')
_val = tblock.getWeight()
_weight = None
if _val != TextStyle.WEIGHT_NORMAL:
if _val == TextStyle.WEIGHT_LIGHT:
_weight = 'light'
elif _val == TextStyle.WEIGHT_BOLD:
_weight = 'bold'
elif _val == TextStyle.WEIGHT_HEAVY:
_weight = 'heavy'
else:
raise ValueError, "Unexpected font weight: %d" % _val
_val = tblock.getStyle()
_style = None
if _val != TextStyle.FONT_NORMAL:
if _val == TextStyle.FONT_OBLIQUE:
_style = 'oblique'
elif _val == TextStyle.FONT_ITALIC:
_style = 'italic'
else:
raise ValueError, "Unexpected font style: %d" % _val
if _weight is None and _style is None:
_font = _family
else:
if _weight is None:
_font = "%s-%s" % (_family, _style)
elif _style is None:
_font = "%s-%s" % (_family, _weight)
else:
_font = "%s-%s%s" % (_family, _style, _weight)
_tbdata['font'] = _font
_col = None
if self.__color:
_c = tblock.getColor()
if self.__invert and _c == Plot.__white:
_col = (0, 0, 0) # black
else:
_col = _c.getColors() # r, g, b
_tbdata['color'] = _col
_val = tblock.getAlignment()
if _val == TextStyle.ALIGN_LEFT:
_align = 'left'
elif _val == TextStyle.ALIGN_CENTER:
_align = 'center'
elif _val == TextStyle.ALIGN_RIGHT:
_align = 'right'
else:
raise ValueError, "Unexpected alignment: %d" % _val
_tbdata['align'] = _align
_tbdata['size'] = tblock.getSize()
_tbdata['angle'] = tblock.getAngle()
_tbdata['text'] = tblock.getText().splitlines()
# return (_x, _y, _bounds, _font, _col, _align, _size, _angle, _text)
return _tbdata
def _saveDimensionData(self, dim):
if isinstance(dim, dimension.LinearDimension):
self._saveLDimData(dim)
elif isinstance(dim, dimension.RadialDimension):
self._saveRDimData(dim)
elif isinstance(dim, dimension.AngularDimension):
self._saveADimData(dim)
else:
raise TypeError, "Unexpected dimension type: " + `dim`
def _getDimGraphicData(self, dim):
_col = None
if self.__color:
_c = dim.getColor()
if self.__invert and _c == Plot.__white:
_col = (0, 0, 0) # black
else:
_col = _c.getColors() # r, g, b
return _col
def _getDimMarkers(self, dim):
_data = {}
_etype = dim.getEndpointType()
if isinstance(dim, dimension.AngularDimension):
_crossbar = dim.getDimCrossarc()
else:
_crossbar = dim.getDimCrossbar()
_mp1, _mp2 = _crossbar.getCrossbarPoints()
if (_etype == dimension.Dimension.DIM_ENDPT_ARROW or
_etype == dimension.Dimension.DIM_ENDPT_FILLED_ARROW or
_etype == dimension.Dimension.DIM_ENDPT_SLASH):
_epts = _crossbar.getMarkerPoints()
_data['p1'] = _epts[0]
_data['p2'] = _epts[1]
_data['p3'] = _epts[2]
_data['p4'] = _epts[3]
_data['v1'] = _mp1
_data['v2'] = _mp2
if _etype == dimension.Dimension.DIM_ENDPT_ARROW:
_data['type'] = 'arrow'
elif _etype == dimension.Dimension.DIM_ENDPT_FILLED_ARROW:
_data['type'] = 'filled_arrow'
elif _etype == dimension.Dimension.DIM_ENDPT_SLASH:
_data['type'] = 'slash'
elif _etype == dimension.Dimension.DIM_ENDPT_CIRCLE:
_data['type'] = 'circle'
_data['radius'] = dim.getEndpointSize()
_data['c1'] = _mp1
_data['c2'] = _mp2
elif _etype == dimension.Dimension.DIM_ENDPT_NONE:
_data['type'] = None
else:
raise ValueError, "Unexpected endpoint type: %d" % _etype
return _data
def _saveLDimData(self, dim):
_dimdata = {}
_bar1, _bar2 = dim.getDimBars()
_ep1, _ep2 = _bar1.getEndpoints()
_dimdata['ep1'] = _ep1
_dimdata['ep2'] = _ep2
_ep3, _ep4 = _bar2.getEndpoints()
_dimdata['ep3'] = _ep3
_dimdata['ep4'] = _ep4
_ep5, _ep6 = dim.getDimCrossbar().getEndpoints()
_dimdata['ep5'] = _ep5
_dimdata['ep6'] = _ep6
_eptype = dim.getEndpointType()
_dimdata['eptype'] = _eptype
_dimdata['color'] = self._getDimGraphicData(dim)
_dimdata['thickness'] = dim.getThickness()
_ds1, _ds2 = dim.getDimstrings()
_dimdata['ds1'] = self._getTextBlockData(_ds1)
if dim.getDualDimMode():
_dimdata['ds2'] = self._getTextBlockData(_ds2)
_dimdata['markers'] = self._getDimMarkers(dim)
_data = self.__data.setdefault('ldims', [])
_data.append(_dimdata)
def _saveRDimData(self, dim):
_dimdata = {}
_ep1, _ep2 = dim.getDimCrossbar().getEndpoints()
_dimdata['ep1'] = _ep1
_dimdata['ep2'] = _ep2
_dimdata['color'] = self._getDimGraphicData(dim)
_dimdata['thickness'] = dim.getThickness()
_ds1, _ds2 = dim.getDimstrings()
_dimdata['dia_mode'] = dim.getDiaMode()
_dimdata['ds1'] = self._getTextBlockData(_ds1)
if dim.getDualDimMode():
_dimdata['ds2'] = self._getTextBlockData(_ds2)
_mdata = self._getDimMarkers(dim)
if not dim.getDiaMode() and _mdata['type'] is not None:
_mdata['rdim'] = True
_dimdata['markers'] = _mdata
_data = self.__data.setdefault('rdims', [])
_data.append(_dimdata)
def _saveADimData(self, dim):
_dimdata = {}
_bar1, _bar2 = dim.getDimBars()
_ep1, _ep2 = _bar1.getEndpoints()
_dimdata['ep1'] = _ep1
_dimdata['ep2'] = _ep2
_ep3, _ep4 = _bar2.getEndpoints()
_dimdata['ep3'] = _ep3
_dimdata['ep4'] = _ep4
_dimdata['vp'] = dim.getVertexPoint().getCoords()
_crossarc = dim.getDimCrossarc()
_r = _crossarc.getRadius()
_dimdata['r'] = _r
_sa = _crossarc.getStartAngle()
_dimdata['sa'] = _sa
_ea = _crossarc.getEndAngle()
_dimdata['ea'] = _ea
_dimdata['color'] = self._getDimGraphicData(dim)
_dimdata['thickness'] = dim.getThickness()
_ds1, _ds2 = dim.getDimstrings()
_dimdata['ds1'] = self._getTextBlockData(_ds1)
if dim.getDualDimMode():
_dimdata['ds2'] = self._getTextBlockData(_ds2)
_dimdata['markers'] = self._getDimMarkers(dim)
_data = self.__data.setdefault('adims', [])
_data.append(_dimdata)
def getPlotEntities(self, entity):
return self.__data[entity]
|