Weird result after doc.recompute()

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
nmt
Posts: 29
Joined: Sun Feb 19, 2017 2:45 pm

Weird result after doc.recompute()

Post by nmt »

Please run the following script

Code: Select all

import Part

doc = FreeCAD.newDocument('Slice')
Gui.activeDocument().activeView().viewAxonometric()
b = Part.makeBox(10,10,10,Base.Vector(0,0,0),Base.Vector(0,0,1))
box = doc.addObject("Part::Box","Box")
box.Shape = b
planes = []
for i in range(2,10,2):
	p = Part.makePlane(14,14, Base.Vector(-2,-2,i), Base.Vector(0,0,1))
	plane = doc.addObject("Part::Plane",("Plane%s" % (i)))
	plane.Shape = p
	planes.append(p)

import Part

doc = FreeCAD.newDocument('Slice')
Gui.activeDocument().activeView().viewAxonometric()
b = Part.makeBox(10,10,10,Base.Vector(0,0,0),Base.Vector(0,0,1))
box = doc.addObject("Part::Box","Box")
box.Shape = b
planes = []
for i in range(2,10,2):
	p = Part.makePlane(14,14, Base.Vector(-2,-2,i), Base.Vector(0,0,1))
	plane = doc.addObject("Part::Plane",("Plane%s" % (i)))
	plane.Shape = p
	planes.append(p)

Gui.SendMsgToActiveView("ViewFit")
You should see a box with several horizontal planes slicing through it. Now, if you add doc.recompute() it goes weird.

I am trying to slice the box with the planes using: r = Part.BOPTools.SplitAPI.slice(b, planes, "Split",tolerance = 0.0), but I cannot get the result I need. Thank you for your help.
Attachments
sliced.png
sliced.png (48.97 KiB) Viewed 1064 times
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Weird result after doc.recompute()

Post by DeepSOIC »

Hi!
This is a completely wrong way of doing it:

Code: Select all

b = Part.makeBox(10,10,10,Base.Vector(0,0,0),Base.Vector(0,0,1))
box = doc.addObject("Part::Box","Box")
box.Shape = b
Part::Box, when recomputes, assigns a new shape to its Shape property, that it makes based on its other properties ("Width", "Height", "Length"). So as soon as you recompute a document, the shape you assigned to Box is lost.

You should either use Part.show(b) to make a dumb object that holds a shape assigned to it forever, or make a Part::Box and assign its width,height,length to 10,10,10 and ask it to recompute. You can also omit adding box shape to document entirely, by directly feeding "b" to Part.BOPTools.slice(...)

Same applies to Plane objects you are creating later in your script.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Weird result after doc.recompute()

Post by DeepSOIC »

Here's one way to right your script:

Code: Select all

import FreeCAD
import Part
from FreeCAD import Base

# compute shapes....
b = Part.makeBox(10,10,10,Base.Vector(0,0,0),Base.Vector(0,0,1))
planes = []
for i in range(2,10,2):
   p = Part.makePlane(14,14, Base.Vector(-2,-2,i), Base.Vector(0,0,1))
   planes.append(p)

r = Part.BOPTools.SplitAPI.slice(b, planes, "Split")
children = r.childShapes()

# Shapes computed. Show results...
doc = App.newDocument()
for child in children:
   Part.show(child)
   doc.ActiveObject.ViewObject.Transparency=50

Gui.SendMsgToActiveView("ViewFit")
User avatar
nmt
Posts: 29
Joined: Sun Feb 19, 2017 2:45 pm

Re: Weird result after doc.recompute()

Post by nmt »

Thank you very much! Sorry, I am new to FreeCAD and OpenCascade and trying to learn the logic of the API.
Post Reply