Arch cut plan with line proposition

A forum dedicated to the Draft, Arch and BIM workbenches development.
User avatar
petit_chat_noir
Posts: 86
Joined: Wed Oct 02, 2019 6:45 pm

Arch cut plan with line proposition

Postby petit_chat_noir » Sun Nov 03, 2019 9:38 am

Hello,
I've wrote a new function in Arch cutPlan. It use only a Draft.line or shape's edge and it create a temporary face with the workingPlan normal.
So i think it's more easy to cut somes parts with it ! :)

Code: Select all


import FreeCAD,ArchCommands
from FreeCAD import Vector
if FreeCAD.GuiUp:
    import FreeCADGui,Part
    from PySide import QtCore, QtGui
    from DraftTools import translate
else:
    # \cond
    def translate(ctxt,txt):
        return txt
    # \endcond

__title__="FreeCAD CutPlane"
__author__ = "Jonathan Wiedemann"
__url__ = "http://www.freecadweb.org"

## @package ArchCutPlane
#  \ingroup ARCH
#  \brief The Cut plane object and tools
#
#  This module handles the Cut Plane object

def getPlanWithLine(line):
    """Function to make a plane along Normal plan"""
    plan= FreeCAD.DraftWorkingPlane
    w=plan.getNormal()
    part=Part.Shape(line)
    out=part.extrude(w)
    return out


def cutComponentwithPlane(archObject, cutPlane, sideFace):
    """cut object from a plan define by a face, Behind = 0 , front = 1"""
    cutVolume = ArchCommands.getCutVolume(cutPlane, archObject.Object.Shape)
    if sideFace == 0:
        cutVolume = cutVolume[2]
    else:
        cutVolume = cutVolume[1]
    if cutVolume:
        obj = FreeCAD.ActiveDocument.addObject("Part::Feature","CutVolume")
        obj.Shape = cutVolume
        obj.ViewObject.ShapeColor = (1.00,0.00,0.00)
        obj.ViewObject.Transparency = 75
        if "Additions" in archObject.Object.PropertiesList:
            return ArchCommands.removeComponents(obj,archObject.Object)
        else:
            cutObj = FreeCAD.ActiveDocument.addObject("Part::Cut","CutPlane")
            cutObj.Base = archObject.Object
            cutObj.Tool = obj
            return cutObj


class _CommandCutLine:
    "the Arch CutPlane command definition"
    def GetResources(self):
       return {'Pixmap'  : 'Arch_CutLine',
                'MenuText': QtCore.QT_TRANSLATE_NOOP("Arch_CutPlane","Cut with a line"),
                'ToolTip': QtCore.QT_TRANSLATE_NOOP("Arch_CutPlane","Cut an object with a line with normal workplane")}

    def IsActive(self):
        return len(FreeCADGui.Selection.getSelection()) > 1

    def Activated(self):
        sel = FreeCADGui.Selection.getSelectionEx()
        if len(sel) != 2:
            FreeCAD.Console.PrintError("You must select exactly two objects, the shape to be cut and a line\n")
            return
        if not sel[1].SubObjects:
            FreeCAD.Console.PrintError("You must select a line from the second object (cut line), not the whole object\n")
            return
        panel=_CutPlaneTaskPanel(linecut=True)
        FreeCADGui.Control.showDialog(panel)

class _CommandCutPlane:
    "the Arch CutPlane command definition"
    def GetResources(self):
       return {'Pixmap'  : 'Arch_CutPlane',
                'MenuText': QtCore.QT_TRANSLATE_NOOP("Arch_CutPlane","Cut with plane"),
                'ToolTip': QtCore.QT_TRANSLATE_NOOP("Arch_CutPlane","Cut an object with a plane")}

    def IsActive(self):
        return len(FreeCADGui.Selection.getSelection()) > 1

    def Activated(self):
        sel = FreeCADGui.Selection.getSelectionEx()
        if len(sel) != 2:
            FreeCAD.Console.PrintError("You must select exactly two objects, the shape to be cut and the cut plane\n")
            return
        if not sel[1].SubObjects:
            FreeCAD.Console.PrintError("You must select a face from the second object (cut plane), not the whole object\n")
            return
        panel=_CutPlaneTaskPanel()
        FreeCADGui.Control.showDialog(panel)

class _CutPlaneTaskPanel:
    def __init__(self,linecut=False):
        self.linecut=linecut
        self.plan=None
        if linecut:
            self.plan=plan=getPlanWithLine(FreeCADGui.Selection.getSelectionEx()[1].SubObjects[0])
        else :
            self.plan = FreeCADGui.Selection.getSelectionEx()[1].SubObjects[0]

        self.form = QtGui.QWidget()
        self.form.setObjectName("TaskPanel")
        self.grid = QtGui.QGridLayout(self.form)
        self.grid.setObjectName("grid")
        self.title = QtGui.QLabel(self.form)
        self.grid.addWidget(self.title, 1, 0)
        self.infoText =  QtGui.QLabel(self.form)
        self.grid.addWidget(self.infoText, 2, 0)
        self.combobox = QtGui.QComboBox()
        self.combobox.setCurrentIndex(0)
        self.grid.addWidget(self.combobox, 2, 1)
        QtCore.QObject.connect(self.combobox,QtCore.SIGNAL("currentIndexChanged(int)"),self.previewCutVolume)
        self.previewObj = FreeCAD.ActiveDocument.addObject("Part::Feature","PreviewCutVolume")
        self.retranslateUi(self.form)
        self.previewCutVolume(self.combobox.currentIndex())

    def isAllowedAlterSelection(self):
        return False

    def accept(self):
        FreeCAD.ActiveDocument.removeObject(self.previewObj.Name)
        val = self.combobox.currentIndex()
        s = FreeCADGui.Selection.getSelectionEx()
        if len(s) > 1:
            if s[1].SubObjects:
                FreeCAD.ActiveDocument.openTransaction(translate("Arch","Cutting"))
                FreeCADGui.addModule("Arch")
                ###TODO redo FreeCADGui.doCommand by using self.plan:
                #FreeCADGui.doCommand("Arch.cutComponentwithPlane(FreeCADGui.Selection.getSelectionEx()[0],self.plan,"+ str(val) +")")
                cutComponentwithPlane(FreeCADGui.Selection.getSelectionEx()[0],self.plan,val)

                FreeCAD.ActiveDocument.commitTransaction()
                FreeCAD.ActiveDocument.recompute()
                return True
        FreeCAD.Console.PrintError("Wrong selection\n")
        return True

    def reject(self):
        FreeCAD.ActiveDocument.removeObject(self.previewObj.Name)
        FreeCAD.Console.PrintMessage("Cancel Cut Plane\n")
        return True

    def getStandardButtons(self):
        return int(QtGui.QDialogButtonBox.Ok|QtGui.QDialogButtonBox.Cancel)

    def previewCutVolume(self, i):
        cutVolume = ArchCommands.getCutVolume(self.plan,FreeCADGui.Selection.getSelectionEx()[0].Object.Shape)
        FreeCAD.ActiveDocument.removeObject(self.previewObj.Name)
        self.previewObj = FreeCAD.ActiveDocument.addObject("Part::Feature", "PreviewCutVolume")
        self.previewObj.ViewObject.ShapeColor = (1.00,0.00,0.00)
        self.previewObj.ViewObject.Transparency = 75
        if i == 1:
            cutVolume = cutVolume[1]
        else:
            cutVolume = cutVolume[2]
        if cutVolume:
            self.previewObj.Shape = cutVolume

    def retranslateUi(self, TaskPanel):
        TaskPanel.setWindowTitle(QtGui.QApplication.translate("Arch", "Cut Plane", None))
        self.title.setText(QtGui.QApplication.translate("Arch", "Cut Plane options", None))
        self.infoText.setText(QtGui.QApplication.translate("Arch", "Which side to cut", None))
        self.combobox.addItems([QtGui.QApplication.translate("Arch", "Behind", None),
                                    QtGui.QApplication.translate("Arch", "Front", None)])

if FreeCAD.GuiUp:
    FreeCADGui.addCommand('Arch_CutPlane',_CommandCutPlane())
    FreeCADGui.addCommand('Arch_CutLine', _CommandCutLine())

tell me if you think it's good or not

In the futur i would like to cut a part with poly line extruded from the Workingplan Normal, it should be very helpful !!
edit:polyline doesn't exist in freecad but it's Wire

edit edit !: I've made some screenshots
Screenshot_20191104_133951.png
Screenshot_20191104_133951.png (20.73 KiB) Viewed 451 times
Screenshot_20191104_134042.png
Screenshot_20191104_134042.png (23.59 KiB) Viewed 451 times
Screenshot_20191104_134100.png
Screenshot_20191104_134100.png (21.32 KiB) Viewed 451 times
Attachments
ArchCutPlane.py
(8.47 KiB) Downloaded 11 times
User avatar
yorik
Site Admin
Posts: 12065
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels, Belgium
Contact:

Re: Arch cut plan with line proposition

Postby yorik » Mon Nov 04, 2019 6:49 pm

This looks very good! Mind to make a PR? ;)
User avatar
petit_chat_noir
Posts: 86
Joined: Wed Oct 02, 2019 6:45 pm

Re: Arch cut plan with line proposition

Postby petit_chat_noir » Thu Nov 07, 2019 1:27 pm

Yes I will, but have I to separate the two functions in two files ?
like archcutplane.py and archWPcutline.py ?
User avatar
yorik
Site Admin
Posts: 12065
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels, Belgium
Contact:

Re: Arch cut plan with line proposition

Postby yorik » Tue Nov 12, 2019 2:20 pm

Sorry, forgot to reply... Your PR seems good. I think the rule is more, if you add a lot of code it's best to make a separate file, otherwise it's best to add to an existing one. But what you did is fine