Skip to content

Commit

Permalink
Insert-any-shape tool
Browse files Browse the repository at this point in the history
  • Loading branch information
oddtopus committed Jul 29, 2018
1 parent b9dd316 commit 800df28
Show file tree
Hide file tree
Showing 10 changed files with 94,336 additions and 1 deletion.
12 changes: 12 additions & 0 deletions CommandsPipe.py
Expand Up @@ -229,6 +229,17 @@ def Activated(self):
def GetResources(self):
return{'Pixmap':os.path.join(os.path.dirname(os.path.abspath(__file__)),"icons","point2point.svg"),'MenuText':'draw a tube point-to-point','ToolTip':'Click on subsequent points.'}

class insertAny:
'''
Dialog to insert any object saved as .STEP, .IGES or .BREP in folder ../Mod/flamingo/shapes or subfolders.
'''
def Activated(self):
import anyShape
FreeCADGui.Control.showDialog(anyShape.shapeDialog())

def GetResources(self):
return{'MenuText':'Insert any shape','ToolTip':'Insert a STEP, IGES or BREP'}

#---------------------------------------------------------------------------
# Adds the commands to the FreeCAD command manager
#---------------------------------------------------------------------------
Expand All @@ -251,3 +262,4 @@ def GetResources(self):
addCommand('laydown',laydown())
addCommand('raiseup',raiseup())
addCommand('point2point',point2point())
addCommand('insertAny',insertAny())
2 changes: 1 addition & 1 deletion InitGui.py
Expand Up @@ -120,7 +120,7 @@ def Initialize(self):
self.appendToolbar("frameTools",list4)
Log ('Loading Frame tools: done\n')
import CommandsPipe
list5=["insertPipe","insertElbow","insertReduct","insertCap","insertValve","insertFlange","insertUbolt","insertPypeLine","insertBranch","breakPipe","mateEdges","joinPype","flat","extend2intersection","extend1intersection","laydown","raiseup","attach2tube","point2point"]
list5=["insertPipe","insertElbow","insertReduct","insertCap","insertValve","insertFlange","insertUbolt","insertPypeLine","insertBranch","breakPipe","mateEdges","joinPype","flat","extend2intersection","extend1intersection","laydown","raiseup","attach2tube","point2point","insertAny"]
self.appendToolbar("pipeTools",list5)
Log ('Loading Pipe tools: done\n')
menu1 = ["Frame tools"]
Expand Down
138 changes: 138 additions & 0 deletions anyShape.py
@@ -0,0 +1,138 @@
import FreeCAD, FreeCADGui, Part
import frameCmd
from os.path import *
from os import listdir
from pipeFeatures import pypeType

class AnyThing(pypeType): #object):
'''Class for object TType="Anything"
AnyThing(obj,name="Thing", fileName="", ports="0:0:0")
obj: the "App::FeaturePython object"
TType (string): name of the thing
FileName (string): a valid .STEP .IGES or .brep
ports: a string in the form "x0:y0:z0/x1:y1:z1/..." representing the position of ports 0,1....
'''
def __init__(self, obj,name="valve",fileName='ballDN15.stp',ports='0:0:0'):
#obj.Proxy = self
super(AnyThing,self).__init__(obj)
# define common properties
obj.PType="Any"
# define specific properties
obj.addProperty("App::PropertyString","FileName","AnyThing","The file of the shape (inside ./shapes)").FileName=fileName
portslist=list()
if ports:
for port in ports.split('/'):
portslist.append(FreeCAD.Vector([float(i) for i in port.split(":")]))
obj.Ports=portslist
if fileName:
s=Part.Shape()
path=join(dirname(abspath(__file__)),"shapes",fileName)
if exists(path):
s.read(path)
obj.Shape=s
else:
FreeCAD.Console.PrintError("%s file doesn't exist" %fileName)
def onChanged(self, fp, prop):
pass
def execute(self, fp):
super(AnyThing,self).execute(fp) # perform common operations

def makeThing(n='Valvola', fn='ballDN15.stp', p='0:0:0', pos=None, Z=None):
'''
makeThing(n,fn,p,pos,Z)
n = name
fn = file name
p = ports string (e.g. "0:0:0/0:0:69")
pos = position Vector
Z = orientation Vector
'''
if pos==None:
pos=FreeCAD.Vector(0,0,0)
if Z==None:
Z=FreeCAD.Vector(0,0,1)
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython",n)
AnyThing(a, name=n, fileName=fn, ports=p)
a.ViewObject.Proxy=0
a.Placement.Base=pos
rot=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),Z)
a.Placement.Rotation=rot.multiply(a.Placement.Rotation)
return a

from frameForms import prototypeDialog

class shapeDialog(prototypeDialog):
'dialog for any()'
def __init__(self):
super(shapeDialog,self).__init__('shapes.ui')
self.shapesDir=join(dirname(abspath(__file__)),'shapes')
self.cwd=self.shapesDir
self.pipeDictList=list()
self.filesListed=list()
self.lastThing=None
for n in listdir(self.cwd):
absFile=join(self.cwd,n)
if isdir(absFile): self.form.comboDirs.addItem(n)
elif isfile(absFile)and n.upper().split(extsep)[-1] in ['STEP','STP','IGES','IGS','BREP','BRP']: self.form.listFiles.addItem(n)
self.form.listFiles.setCurrentRow(0)
self.form.comboDirs.currentIndexChanged.connect(self.fillFiles)
self.form.listFiles.itemClicked.connect(self.checkListed)
def accept(self):
fname=self.form.listFiles.currentItem().text()
row=None
pos=None
Z=None
if self.form.comboDirs.currentText()=='<shapes>': sdir=''
else: sdir=self.form.comboDirs.currentText()
if fname in self.filesListed:
for r in self.pipeDictList:
if r["fileName"]==fname:
row=r
break
name=row["name"]
ports=row["ports"]
else:
name=fname.split('.')[0]
ports='0:0:0'
selex=FreeCADGui.Selection.getSelectionEx()
if selex:
vxs=[v for sx in selex for so in sx.SubObjects for v in so.Vertexes]
cedges=[e for e in frameCmd.edges() if e.curvatureAt(0)!=0]
faces=frameCmd.faces()
if faces:
x=(faces[0].ParameterRange[0]+faces[0].ParameterRange[1])/2
y=(faces[0].ParameterRange[2]+faces[0].ParameterRange[3])/2
pos=faces[0].valueAt(x,y)
Z=faces[0].normalAt(x,y)
elif cedges:
pos=cedges[0].centerOfCurvatureAt(0)
Z=cedges[0].tangentAt(0).cross(cedges[0].normalAt(0))
elif vxs:
pos=vxs[0].Point
self.lastThing=makeThing(name,join(sdir,fname),ports,pos,Z)
if row:
self.lastThing.Kv=float(row["Kv"])
self.lastThing.PSize=row["DN"]
self.lastThing.PRating=row["PN"]
def fillFiles(self):
self.form.listFiles.clear()
self.form.labListed.setText('-')
if self.form.comboDirs.currentText()!='<shapes>':
self.cwd=join(self.shapesDir,self.form.comboDirs.currentText())
else:
self.cwd=self.shapesDir
for n in listdir(self.cwd):
if isfile(join(self.cwd,n)) and n.upper().split(extsep)[-1] in ['STEP','STP','IGES','IGS','BREP','BRP']:
self.form.listFiles.addItem(n)
if n[:4]=='Any_' and n[-4:]=='.csv':
f=open(join(self.cwd,n),'r')
from csv import DictReader
reader=DictReader(f,delimiter=';')
self.pipeDictList=[DNx for DNx in reader]
f.close()
self.filesListed=[row['fileName'] for row in self.pipeDictList]
self.form.listFiles.setCurrentRow(0)
def checkListed(self):
if self.form.listFiles.currentItem().text() in self.filesListed:
self.form.labListed.setText('*** LISTED ***')
else:
self.form.labListed.setText('(unlisted)')
43 changes: 43 additions & 0 deletions dialogs/shapes.ui
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Import shapes</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QComboBox" name="comboDirs">
<item>
<property name="text">
<string>&lt;shapes&gt;</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QListWidget" name="listFiles"/>
</item>
<item>
<widget class="QLabel" name="labListed">
<property name="text">
<string>-</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
2 changes: 2 additions & 0 deletions pipeFeatures.py
Expand Up @@ -9,6 +9,7 @@

import FreeCAD, FreeCADGui, Part, frameCmd, pipeCmd
from copy import copy
from os.path import join, dirname, abspath

################ CLASSES ###########################

Expand Down Expand Up @@ -636,3 +637,4 @@ def purge(self,fp):
if hasattr(fp,'Curves'):
for name in fp.Curves: FreeCAD.ActiveDocument.removeObject(name)
fp.Curves=[]

3 changes: 3 additions & 0 deletions shapes/ballValves/Any_valves.csv
@@ -0,0 +1,3 @@
name;fileName;DN;PN;Kv;ports
"ball valve 1/2""";ballDN15.stp;DN15;PN10;10;0:0:0/0:0:69
"ball valve 3/4""";ballDN20.step;DN20;PN10;15;0:0:0/0:0:77

0 comments on commit 800df28

Please sign in to comment.