FeaturePython shape, union sphere and cylinder

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
fosselius
Posts: 381
Joined: Sat Apr 23, 2016 10:03 am
Contact:

FeaturePython shape, union sphere and cylinder

Post by fosselius »

I want to create a visual shape in a FeaturePython that consists of a union of a cylinder and a sphere, what is the best way to achieve this?
The shape is going to visualize a joints orientation and limits in my "RobotCreator" workbench.

Currently i only have a sphere that is implemented like this:

Code: Select all


def execute(self, fp):
	fp.Shape = Part.makeSphere(1)
	
In the FreeCAD python terminal i can run:

Code: Select all

App.ActiveDocument.addObject("Part::Sphere","Sphere")
App.ActiveDocument.addObject("Part::Cylinder","Cylinder")
FreeCAD.getDocument("Unnamed").getObject("Cylinder").Angle = 180.00
App.activeDocument().Fusion.Shapes = [App.activeDocument().Sphere,App.activeDocument().Cylinder,]
shapeThatIWant = FreeCAD.getDocument("Unnamed").getObject("Fusion").Shape
// remove sphere
// remove cylinder
// remove fusion
But it feels a bit silly to create and delete objects in the active document just to get the shape i need..

Screenshot from 2017-11-03 08-22-52.png
Screenshot from 2017-11-03 08-22-52.png (33.14 KiB) Viewed 645 times
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: FeaturePython shape, union sphere and cylinder

Post by DeepSOIC »

fosselius wrote: Fri Nov 03, 2017 8:11 am Currently i only have a sphere that is implemented like this:

Code: Select all


def execute(self, fp):
	fp.Shape = Part.makeSphere(1)
	
Looks like you are on a right track.
fp.Shape = Part.makeSphere(1).fuse(Part.makeCylinder(...)) and so on

If the shape is the same all the time, it might be a good idea to load up a brep file instead of computing the shape every time.

Or you might want to create a visual-only shape for this purpose. Then you should use Coin (from pivy import coin), create stuff with it and add it to node of viewprovider of your feature. That is outside of my comfort zone, for sure. There is an example here: Scripted objects
User avatar
fosselius
Posts: 381
Joined: Sat Apr 23, 2016 10:03 am
Contact:

Re: FeaturePython shape, union sphere and cylinder

Post by fosselius »

Now this was what i was after! Thank you!

Code: Select all

    fp.Shape = Part.makeSphere(1).fuse(Part.makeCylinder(2,0.2,App.Vector(0,0,-0.1),App.Vector(0,0,1),180))
Screenshot from 2017-11-03 13-56-51.png
Screenshot from 2017-11-03 13-56-51.png (22.81 KiB) Viewed 620 times
now its time for pose ;)

Here is an example of the "joint" structure i try to visualize and will later generate from this "joint" object

Code: Select all

<joint name="Joint" type="revolute">
  <parent>Link1</parent>
  <child>Link2</child>
  <pose>0 0 0 0 0 0</pose> // derived from joint object
  <axis>
    <xyz>0 0 1</xyz> // orientation of disc
    <limit>
      <lower>-0.785398</lower> // visualized as pie/disc
      <upper>0.785398</upper> // visualized as pie/disc
    </limit>
  </axis>
 </joint>
Post Reply