Move scripted project with Base project

Need help, or want to share a macro? Post here!
ikua
Posts: 125
Joined: Fri Apr 07, 2017 1:32 pm

Move scripted project with Base project

Postby ikua » Thu Mar 29, 2018 2:55 pm

Hello!

I am working on a script to add Flanges to pipes created with the arch tools as a first step. Late i would like to improve it to add other fittings.
I all ready managed, as a starting point, to add two Cylinders (later on they will be the flanges) do a wire. Now i would like that the flanges (cylinders) move when the dwire is moved or changed. I think therefore i would have to connect the new shape with a App::PropertyLink in the init part and based on that calculate the position of the flanges. But i was not able to put the selected dwire in to the Property list on inti. Maybe somebody has a solution here.

Code: Select all

import FreeCAD as App
import FreeCADGui
import FreeCAD
import Part
import math
# Flansche richten sich nach linien aus
#das ganze muss aber noch mit dem dwire verknpüft werden

o=Gui.Selection.getSelection()[0]
print(o.TypeId)
print o.Label
print ('hello')
obj= o.Shape #das ist ein wire (muss eins sein)
print obj.Edges
print "Vertices: "
v0=obj.Vertexes[0].Point
r0=obj.Vertexes[0].Point.sub(obj.Vertexes[1].Point)
vE=obj.Vertexes[-1].Point
rE=obj.Vertexes[-1].Point.sub(obj.Vertexes[-2].Point)
print r0,rE


class Line:
	def __init__(self, obj):
		'''Add some custom properties to our box feature'''
		obj.addProperty("App::PropertyLength","d","Flansch","Durchmesser in mm").d=220
		obj.addProperty("App::PropertyLink","Base","Component")
		#obj.addProperty("App::PropertyLength","Width","Box","Width of the box").Width=1.0
		#obj.addProperty("App::PropertyLength","Height","Box", "Height of the box").Height=1.0
		obj.Proxy = self

	def execute(self, fp):
		'''"Print a short message when doing a recomputation, this method is mandatory" '''
		#fp.Shape = Part.makeCylinder(fp.d,20,v,r,0)
		FlanschA = Part.makeCylinder(fp.d/2,20,v0,r0)
		FlanschE = Part.makeCylinder(fp.d/2,20,vE,rE)
		comp=Part.Compound([FlanschA,FlanschE])
		#print comp.TypeId
		#print dir(comp)
		fp.Shape = comp

class ViewProviderLine:
	def __init__(self, obj):
		''' Set this object to the proxy object of the actual view provider '''
		obj.Proxy = self

	def getDefaultDisplayMode(self):
		''' Return the name of the default display mode. It must be defined in getDisplayModes. '''
		return "Flat Lines"

	def updateData(self, fp, prop):
		'''If a property of the handled feature has changed we have the chance to handle this here'''
		# fp is the handled feature, prop is the name of the property that has changed
		#d = fp.getPropertyByName("Durchmesser")
		#self.scale.scaleFactor.setValue(float(d))
		pass

a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Flansch")
Line(a)
ViewProviderLine(a.ViewObject)
App.ActiveDocument.recompute() 

greets ikua
ikua
Posts: 125
Joined: Fri Apr 07, 2017 1:32 pm

Re: Move scripted project with Base project

Postby ikua » Thu Mar 29, 2018 3:12 pm

sorted it out myself. Here the solution if somebody needs it
the script must be executed with a wire selected

Code: Select all

import FreeCAD as App
import FreeCADGui
import FreeCAD
import Part
# noch anpassen fuer weitere armaturen

o=Gui.Selection.getSelection()[0]
print(o.TypeId)
print o.Label
print ('hello')
obj= o.Shape #das ist ein wire (muss eins sein)
print obj.Edges
print "Vertices: "
#print r0,rE

class Line:
	def __init__(self, obj):
		'''Add some custom properties to our box feature'''
		obj.addProperty("App::PropertyLength","d","Flansch","Durchmesser in mm").d=220
		obj.addProperty("App::PropertyLink","Base","Component").Base =Gui.Selection.getSelection()[0]
		obj.Proxy = self

	def execute(self, fp):
		'''"Print a short message when doing a recomputation, this method is mandatory" '''
		#fp.Shape = Part.makeCylinder(fp.d,20,v,r,0)
		v0=fp.Base.Shape.Vertexes[0].Point
		r0=fp.Base.Shape.Vertexes[0].Point.sub(fp.Base.Shape.Vertexes[1].Point)
		vE=fp.Base.Shape.Vertexes[-1].Point
		rE=fp.Base.Shape.Vertexes[-1].Point.sub(fp.Base.Shape.Vertexes[-2].Point)
		FlanschA = Part.makeCylinder(fp.d/2,20,v0,r0)
		FlanschE = Part.makeCylinder(fp.d/2,20,vE,rE)
		comp=Part.Compound([FlanschA,FlanschE])
		#print comp.TypeId
		#print dir(comp)
		fp.Shape = comp

class ViewProviderLine:
	def __init__(self, obj):
		''' Set this object to the proxy object of the actual view provider '''
		obj.Proxy = self

	def getDefaultDisplayMode(self):
		''' Return the name of the default display mode. It must be defined in getDisplayModes. '''
		return "Flat Lines"

	def updateData(self, fp, prop):
		'''If a property of the handled feature has changed we have the chance to handle this here'''
		# fp is the handled feature, prop is the name of the property that has changed
		#d = fp.getPropertyByName("Durchmesser")
		#self.scale.scaleFactor.setValue(float(d))
		pass

a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Flansch")
Line(a)
ViewProviderLine(a.ViewObject)
App.ActiveDocument.recompute() 
User avatar
Kunda1
Posts: 4812
Joined: Thu Jan 05, 2017 9:03 pm

Re: Move scripted project with Base project

Postby Kunda1 » Thu Mar 29, 2018 11:37 pm

oddtopus wrote:perhaps you'd be interested in this?
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features