File size: 8,567 Bytes
985c397 | 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 | # ***************************************************************************
# * Copyright (c) 2022 Wanderer Fan <wandererfan@gmail.com> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program 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 Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
"""Provides utility functions for TD Tools."""
import FreeCAD as App
import FreeCADGui as Gui
import Part
from PySide.QtCore import QT_TRANSLATE_NOOP
from PySide import QtGui
from PySide.QtGui import QMessageBox
def havePage():
objs = App.ActiveDocument.Objects
for o in objs:
if o.isDerivedFrom("TechDraw::DrawPage"):
return True
return False
def haveView():
objs = App.ActiveDocument.Objects
for o in objs:
if o.isDerivedFrom("TechDraw::DrawView"):
return True
return False
def displayMessage(title,message):
'''
displayMessage('Title','Messagetext')
'''
msgBox = QtGui.QMessageBox()
msgBox.setText(message)
msgBox.setWindowTitle(title)
msgBox.exec_()
def getSelView(nSel=0):
'''
view = getSelView()
nSel=0 ... number of selected view, 0 = first selected
Return selected view, otherwise return False
'''
if not Gui.Selection.getSelection():
displayMessage('TechDraw_Utils','No view selected')
else:
view = Gui.Selection.getSelection()[nSel]
return view
def getSelVertexes(nVertex=1, nSel=0):
'''
vertexes = getSelVertexes(nVertex)
nVertex=1 ... min. number of selected vertexes
nSel=0 ... number of selected view, 0 = first selected
Return a list of selected vertexes if at least nVertex vertexes are selected, otherwise return False
'''
if getSelView(nSel):
view = getSelView(nSel)
else:
return False
if not Gui.Selection.getSelectionEx():
displayMessage('TechDraw_Utils',
QT_TRANSLATE_NOOP('TechDraw_Utils','No vertex selected'))
return False
objectList = Gui.Selection.getSelectionEx()[nSel].SubElementNames
vertexes = []
for objectString in objectList:
if objectString[0:6] == 'Vertex':
vertexes.append(view.getVertexBySelection(objectString))
if (len(vertexes) < nVertex):
displayMessage('TechDraw_Utils',
QT_TRANSLATE_NOOP('TechDraw_Utils','Select at least ')+
str(nVertex)+
QT_TRANSLATE_NOOP('TechDraw_Utils',' vertexes'))
return False
else:
return vertexes
def getSelEdges(nEdge=1, nSel=0):
'''
edges = getSelEdges(nEdge)
nEdge=1 ... min. number of selected edges
nSel=0 ... number of selected view, 0 = first selected
Return a list of selected edges if at least nedge edges are selected, otherwise return False
'''
if getSelView(nSel):
view = getSelView(nSel)
else:
return False
if not Gui.Selection.getSelectionEx():
displayMessage('TechDraw_Utils',
QT_TRANSLATE_NOOP('TechDraw_Utils','No edge selected'))
return False
objectList = Gui.Selection.getSelectionEx()[nSel].SubElementNames
edges = []
for objectString in objectList:
if objectString[0:4] == 'Edge':
edges.append(view.getEdgeBySelection(objectString))
if (len(edges) < nEdge):
displayMessage('TechDraw_Utils',
QT_TRANSLATE_NOOP('TechDraw_Utils','Select at least ')+
str(nEdge)+
QT_TRANSLATE_NOOP('TechDraw_Utils',' edges'))
return False
else:
return edges
def getCoordinateVectors(view):
'''
(px,py,pz) = getCoordinateVectors(view)
view ... selected view
(px,py,pz) ... returned tuple of vectors (App.Vector)
calculate projected vectors of x-, y- and z-axis
'''
diagonal = view.Direction
xDirection = view.XDirection
origin = App.Vector()
xAxisProj = App.Vector(1,0,0).projectToPlane(origin,diagonal)
yAxisProj = App.Vector(0,1,0).projectToPlane(origin,diagonal)
zAxisProj = App.Vector(0,0,1).projectToPlane(origin,diagonal)
# create wire polygon and roll back in 3D
wire3D = Part.makePolygon([xAxisProj,yAxisProj,zAxisProj,xDirection,xAxisProj])
rotation3D = App.Rotation(diagonal,App.Vector(0,0,1))
wire3D.Placement.Rotation = rotation3D
px = wire3D.Edges[0].Vertexes[0].Point
py = wire3D.Edges[1].Vertexes[0].Point
pz = wire3D.Edges[2].Vertexes[0].Point
pDir = wire3D.Edges[3].Vertexes[0].Point
pDir.z = 0.0
# rotate inside drawing plane
wire2D = Part.makePolygon([px,py,pz,px])
rotation2D = App.Rotation(pDir,App.Vector(1,0,0))
wire2D.Placement.Rotation = rotation2D
px = wire2D.Edges[0].Vertexes[0].Point
py = wire2D.Edges[1].Vertexes[0].Point
pz = wire2D.Edges[2].Vertexes[0].Point
return (px,py,pz)
# +/- the same as getSelVertexes, but returns the vertex name instead of the vertex object.
def getSelVertexNames(nVertex=1, nSel=0):
'''
vertexNames = getSelVertexNames(nVertex)
nVertex=1 ... min. number of selected vertexes
nSel=0 ... number of selected view, 0 = first selected
Return a list of the names of the selected vertexes if at least nVertex vertexes are selected, otherwise return False
'''
view = getSelView(nSel)
if not view:
return False
if not Gui.Selection.getSelectionEx():
displayMessage('TechDraw_Utils',
QT_TRANSLATE_NOOP('TechDraw_Utils','No vertex selected'))
return False
objectList = Gui.Selection.getSelectionEx()[nSel].SubElementNames
vertexNames = []
for objectString in objectList:
if objectString[0:6] == 'Vertex':
vertexNames.append(objectString)
if (len(vertexNames) < nVertex):
displayMessage('TechDraw_Utils',
QT_TRANSLATE_NOOP('TechDraw_Utils','Select at least ')+
str(nVertex)+
QT_TRANSLATE_NOOP('TechDraw_Utils',' vertexes'))
return False
return vertexNames
# +/- the same as getSelEdges, but returns the edge name instead of the vertex object.
def getSelEdgeNames(nEdge=1, nSel=0):
'''
edges = getSelEdgeNames(nEdge)
nEdge=1 ... min. number of selected edges
nSel=0 ... number of selected view, 0 = first selected
Return a list of names for selected edges if at least nedge edges are selected, otherwise return False
'''
view = getSelView(nSel)
if not view:
return False
if not Gui.Selection.getSelectionEx():
displayMessage('TechDraw_Utils',
QT_TRANSLATE_NOOP('TechDraw_Utils','No edge selected'))
return False
objectList = Gui.Selection.getSelectionEx()[nSel].SubElementNames
edgeNames = []
for objectString in objectList:
if objectString[0:4] == 'Edge':
edgeNames.append(objectString)
if (len(edgeNames) < nEdge):
displayMessage('TechDraw_Utils',
QT_TRANSLATE_NOOP('TechDraw_Utils','Select at least ')+
str(nEdge)+
QT_TRANSLATE_NOOP('TechDraw_Utils',' edges'))
return False
else:
return edgeNames
|