forked from hamish2014/FreeCAD_drawing_dimensioning
/
radiusDimension.py
97 lines (85 loc) · 4.78 KB
/
radiusDimension.py
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
from dimensioning import *
import selectionOverlay, previewDimension
from dimensionSvgConstructor import *
d = DimensioningProcessTracker()
def radiusDimensionSVG( center_x, center_y, radius, radialLine_x=None, radialLine_y=None, tail_x=None, tail_y=None, text_x=None, text_y=None, autoPlaceText=False, autoPlaceOffset=2.0,
textFormat_radial='R%3.3f', comma_decimal_place=False,
centerPointDia = 1, arrowL1=3, arrowL2=1, arrowW=2, strokeWidth=0.5, scale=1.0, lineColor='blue', arrow_scheme='auto',
textRenderer=defaultTextRenderer):
XML_body = [ ' <circle cx ="%f" cy ="%f" r="%f" stroke="none" fill="%s" /> ' % (center_x, center_y, centerPointDia*0.5, lineColor) ]
if radialLine_x <> None and radialLine_y <> None:
theta = numpy.arctan2( radialLine_y - center_y, radialLine_x - center_x )
A = numpy.array([ center_x + radius*numpy.cos(theta) , center_y + radius*numpy.sin(theta) ])
B = numpy.array([ center_x - radius*numpy.cos(theta) , center_y - radius*numpy.sin(theta) ])
XML_body.append( svgLine(radialLine_x, radialLine_y, center_x, center_y, lineColor, strokeWidth) )
if radius > 0:
if arrow_scheme <> 'off':
if arrow_scheme == 'auto':
s = 1 if radius > arrowL1 + arrowL2 + 0.5*centerPointDia else -1
elif arrow_scheme == 'in':
s = 1
elif arrow_scheme == 'out':
s = -1
XML_body.append( arrowHeadSVG( A, s*directionVector(A,B), arrowL1, arrowL2, arrowW, lineColor ) )
if tail_x <> None and tail_y <> None:
XML_body.append( svgLine(radialLine_x, radialLine_y, tail_x, radialLine_y, lineColor, strokeWidth) )
text = dimensionText( radius*scale,textFormat_radial, comma=comma_decimal_place)
XML_body.append( textPlacement_common_procedure(numpy.array([radialLine_x, radialLine_y]), numpy.array([tail_x, radialLine_y]), text, text_x, text_y, 0, textRenderer, autoPlaceText, autoPlaceOffset) )
return '<g> %s </g>' % "\n".join(XML_body)
d.dialogWidgets.append( unitSelectionWidget )
d.registerPreference( 'textFormat_radial', 'R%3.3f', 'format mask')
d.registerPreference( 'arrow_scheme')
d.registerPreference( 'autoPlaceText')
d.registerPreference( 'comma_decimal_place')
d.registerPreference( 'centerPointDia')
d.registerPreference( 'arrowL1')
d.registerPreference( 'arrowL2')
d.registerPreference( 'arrowW')
d.registerPreference( 'strokeWidth')
d.registerPreference( 'lineColor')
d.registerPreference( 'textRenderer' )
d.registerPreference( 'autoPlaceOffset')
def radiusDimensionSVG_preview(mouseX, mouseY):
args = d.args + [ mouseX, mouseY ] if len(d.args) < 9 else d.args
return radiusDimensionSVG( *args, scale=d.viewScale*d.unitConversionFactor, **d.dimensionConstructorKWs )
def radiusDimensionSVG_clickHandler( x, y ):
d.args = d.args + [ x, y ]
d.stage = d.stage + 1
if d.stage == 3 and d.dimensionConstructorKWs['autoPlaceText']:
return 'createDimension:%s' % findUnusedObjectName('dim')
if d.stage == 4 :
return 'createDimension:%s' % findUnusedObjectName('dim')
def selectFun( event, referer, elementXML, elementParms, elementViewObject ):
x,y = elementParms['x'], elementParms['y']
debugPrint(2, 'center selected at x=%3.1f y=%3.1f' % (x,y))
d.args = [x, y, elementParms['r']]
d.viewScale = 1/elementXML.rootNode().scaling()
d.stage = 1
selectionOverlay.hideSelectionGraphicsItems()
previewDimension.initializePreview( d, radiusDimensionSVG_preview, radiusDimensionSVG_clickHandler)
maskPen = QtGui.QPen( QtGui.QColor(0,255,0,100) )
maskPen.setWidth(2.0)
maskHoverPen = QtGui.QPen( QtGui.QColor(0,255,0,255) )
maskHoverPen.setWidth(2.0)
class RadiusDimension:
def Activated(self):
V = getDrawingPageGUIVars()
d.activate(V, 'Add Radial Dimension', dialogIconPath=os.path.join( iconPath , 'radiusDimension.svg' ), endFunction=self.Activated)
selectionOverlay.generateSelectionGraphicsItems(
[obj for obj in V.page.Group if not obj.Name.startswith('dim')],
selectFun ,
transform = V.transform,
sceneToAddTo = V.graphicsScene,
doCircles=True, doFittedCircles=True,
maskPen=maskPen,
maskHoverPen=maskHoverPen,
maskBrush = QtGui.QBrush() #clear
)
selectionOverlay.addProxyRectToRescaleGraphicsSelectionItems( V.graphicsScene, V.graphicsView, V.width, V.height)
def GetResources(self):
return {
'Pixmap' : os.path.join( iconPath , 'radiusDimension.svg' ) ,
'MenuText': 'Radius Dimension',
'ToolTip': 'Creates a radius dimension'
}
FreeCADGui.addCommand('dd_radiusDimension', RadiusDimension())