Parabola, scripts and parameters

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
kscheff
Posts: 4
Joined: Wed May 20, 2015 6:26 pm

Re: Parabola, scripts and parameters

Post by kscheff »

Since I didn't figure out how to use the internal Part.Parabola() or Part.ArcofParabola() functions (it misses a more detailed help on the input parameters), I implemented the parabola manually. So its not a nice shape, but it should do it for my 3D Printer 8-)

The code generates a shape. This needs to be converted to a solid shape in the Part WB before you can further use it for bool operations. I looked at the code in the console when doing so and tried to copy the same transformations into the macro, but it doesn't do the same thing.

Code: Select all

import FreeCAD, Part, math, Draft
from FreeCAD import Base

class Antenna:
    def __init__(self, obj):
        ''' Add the properties: Radius, Eccentricity, Height, Segments (see Property View) '''
        print "Antenna::__init__"
        obj.addProperty("App::PropertyLength","Diameter","Antenna","Antenna apreture diameter").Diameter=60.0
        obj.addProperty("App::PropertyLength","Height","Antenna","Height of antenna").Height=20.0
        #obj.addProperty("App::PropertyLength","Focal","Antenna","Focal distance to antenna base").Focal=30.0
        obj.Proxy = self


    def onChanged(self, fp, prop): 
        print "Antenna::onChanged", prop 
        if prop == "Diameter" or prop == "Height" or prop == "Focal": #if one of these is changed
            self.execute(fp)

    def execute(self, fp): #main part of script

        tu = FreeCAD.Units.parseQuantity
        thickness = tu('0.01 mm')
   
        d = fp.Diameter
        #f = fp.Focal
        h = fp.Height + thickness

        a = h / d / d * 4
        f = a/a / 4 / a     # we use a/a to get it into the right units
        print "focal length", f

        steps = 100   
        increment = d / 2 / steps
        y = 0.0
        for i in range(steps):
            x1= i * increment 
            z1= a * x1 * x1 
            x2= x1 + increment
            z2= a * x2 * x2
            seg=Part.makeLine((x1,y,z1),(x2,y,z2))
            if i == 0:
                wire = Part.Wire([seg])
            else:
                wire=Part.Wire([wire,seg])
        
        # move wire back to the center
        seg= Part.makeLine((x2,y,z2), (0.0, y, z2))
        wire = Part.Wire([wire, seg])

        shell=wire.revolve(App.Vector(0,0,0),App.Vector(0,0,1),360)

        # make a solid
        solid=Part.Solid(shell)

        solid = solid.Faces
        solid = Part.Solid(Part.Shell(solid))

        # apply a thickness
        thick=solid.makeThickness([solid.Faces],-thickness,0.001)
        
        #create a small cross  at the focal point
        #c1 = Part.makeLine((-1.0, 0.0,f.Value),  (1.0, 0.0, f.Value))
        #c2 = Part.makeLine(( 0.0,-1.0,f.Value),  (0.0, 1.0, f.Value))
        #c3 = Part.makeLine(( 0.0, 0.0,f.Value-1),(0.0, 0.0, f.Value+1))
        #cross = Part.makeCompound([c1, c2, c3])        

        fp.Shape = thick


def makeAntenna():
    doc = FreeCAD.activeDocument()
    if doc == None:
        doc = FreeCAD.newDocument()
    antenna = doc.addObject("Part::FeaturePython","Antenna") #add object to document
    antenna.Label = "Antenna"
    Antenna(antenna)
    antenna.ViewObject.Proxy=0

if __name__ == "__main__": #feature will be generated after macro execution
    makeAntenna()
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Parabola, scripts and parameters

Post by ickby »

ince I didn't figure out how to use the internal Part.Parabola()
What is the problem with the interface? A Parabola is in its form soley defined by the distance between its focus and the vertex. All parameters of the parabola geometry type assume the vertex to be at (0,0,0). So we can simply do

Code: Select all

p = Part.Parabola()
p.Focus = 2
edge = Part.Edge(p, -3, 3)
Part.show(e)
Note that a parabola is a infinite curve which can not be represented in a edge by default. you can see this by checking the values of

Code: Select all

p.FirstParameter
p.LastParameter
That is why we need to set the parameter range when creating the edge from the geometry. I took arbitrary values. If you want to change the location of the parabola use the location property.

Note that you also can specify 3 points within the parabolas compute function to define the parabola. It is of course a bit strange that one can not set the focus and location in the parabolas constructor as this is different to the other geometric primitives.
kscheff
Posts: 4
Joined: Wed May 20, 2015 6:26 pm

Re: Parabola, scripts and parameters

Post by kscheff »

p.Focus is the resulting focal vector.... and its read only
so the one to set is the focal parameter:

Code: Select all

p = Part.Parabola()
p.Focal = 2
edge = Part.Edge(p, -3, 3)
Part.show(e)
Thanks for original hint to work with Part.Parabola :-)
Post Reply