problem cut with two solid via scripting

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Luixx
Posts: 213
Joined: Thu Jan 25, 2018 9:12 am

problem cut with two solid via scripting

Post by Luixx »

Hi, my english school... my study python and freecad from library freecadzero.
My problem function boolean "cut"

Code: Select all

import FreeCAD, FreeCADGui, Part
App = FreeCAD
Gui = FreeCADGui

class BoolTool():
    def __init__(self, name):
        self.name = name
        self.obj = None

    def cut(self, Base, Tool):
        obj = App.ActiveDocument.addObject("Part::Cut", self.name)
        obj.Base = Base
        obj.Tool = Tool
        obj.Refine = True
        App.ActiveDocument.recompute()
        self.obj = obj
        return obj
My error output:

Code: Select all

<Exception> type must be 'DocumentObject' or 'NoneType', not str
used:

Code: Select all

cut1 = BoolTool("cut1")
cut1.cut("Box1","Cylinder1")
Idea solved problem ??

snippet console freecad python box e cylinder :

Code: Select all

App.activeDocument().addObject("Part::Cut","Cut")
App.activeDocument().Cut.Base = App.activeDocument().Box1
App.activeDocument().Cut.Tool = App.activeDocument().Cylinder1
no problem code

my code problem . any solved problem ?

thanks.
i'am italian my english school.
User avatar
Chris_G
Veteran
Posts: 2598
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: problem cut with two solid via scripting

Post by Chris_G »

Code: Select all

cut1.cut("Box1","Cylinder1")
On the above line, you are calling the cut() function with 2 string arguments.
So, in the 2 lines below, you're assigning obj.Base and obj.Tool to string objects.

Code: Select all

        obj.Base = Base
        obj.Tool = Tool
obj.Base and obj.Tool should be Document objects :

Code: Select all

        obj.Base = App.ActiveDocument.getObject(Base)
        obj.Tool = App.ActiveDocument.getObject(Tool)
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: problem cut with two solid via scripting

Post by onekk »

Hello Luixx,

what Chris_G is telling you is that when you use:

Code: Select all

cut1.cut("Box1","Cylinder1")
you are simply passing the "names" or better string instead of Objects

and that using:

Code: Select all

App.activeDocument().Box1
You are referring to the Object "named" "Box1" in the ActiveDocument (maybe it is the label, by have to investigate, as I don't use this way to pass references)

If it is the name, no problem, ad it is "unmodifiable" (a part of the modifications due to the - and <space> that usually are "translated" to underscore when dealing with "Name" property of and Object)

If it is the Label, this is modifiable so maybe it is no "robust enough" to use as "stable reference".

Why make an entire new thing to do simple a boolean cuts or fusion when it is simple enough to do:

Code: Select all

obj1.cut(obj2).removeSplitter()
to do similar things?

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: problem cut with two solid via scripting

Post by TheMarkster »

onekk wrote: Sun Sep 19, 2021 4:08 pm
Why make an entire new thing to do simple a boolean cuts or fusion when it is simple enough to do:

Code: Select all

obj1.cut(obj2).removeSplitter()
to do similar things?

Depends on the goal. If it is to produce only the final object, your way is better. If it is to produce a document with document objects and their history, his is better.
Post Reply