basic infrastructure for part design primitives

This commit is contained in:
Stefan Tröger
2015-05-10 20:14:10 +02:00
parent 7194413d6b
commit 52c8a19d4d
10 changed files with 632 additions and 1 deletions

View File

@@ -51,6 +51,7 @@
#include "DatumLine.h"
#include "DatumPoint.h"
#include "FeatureBoolean.h"
#include "FeaturePrimitive.h"
namespace PartDesign {
extern PyObject* initModule();
@@ -101,6 +102,10 @@ PyMODINIT_FUNC init_PartDesign()
PartDesign::Line ::init();
PartDesign::Point ::init();
PartDesign::Boolean ::init();
PartDesign::FeaturePrimitive ::init();
PartDesign::Box ::init();
PartDesign::AdditiveBox ::init();
PartDesign::SubtractiveBox ::init();
PartDesign::Point::initHints();
PartDesign::Line ::initHints();

View File

@@ -94,6 +94,8 @@ SET(FeaturesSketchBased_SRCS
FeatureHole.cpp
FeatureBoolean.h
FeatureBoolean.cpp
FeaturePrimitive.h
FeaturePrimitive.cpp
)
SOURCE_GROUP("SketchBasedFeatures" FILES ${FeaturesSketchBased_SRCS})

View File

@@ -0,0 +1,159 @@
/***************************************************************************
* Copyright (c) 2015 Stefan Tröger <stefantroeger@gmx.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#endif
#include "FeaturePrimitive.h"
#include <BRepPrimAPI_MakeBox.hxx>
#include <BRepBuilderAPI_GTransform.hxx>
#include <BRepAlgoAPI_Fuse.hxx>
#include <BRepAlgoAPI_Cut.hxx>
namespace PartDesign {
PROPERTY_SOURCE(PartDesign::FeaturePrimitive, PartDesign::FeatureAddSub)
FeaturePrimitive::FeaturePrimitive()
{
ADD_PROPERTY_TYPE(References, (0,0), "Primitive", (App::PropertyType)(App::Prop_None), "References to build the location of the primitive");
}
App::DocumentObjectExecReturn* FeaturePrimitive::execute(const TopoDS_Shape& primitiveShape)
{
try {
//transform the primitive in the correct coordinance
BRepBuilderAPI_GTransform mkTrf(primitiveShape, getLocation().Transformation());
const TopoDS_Shape primitiveShape = mkTrf.Shape();
//if we have no base we just add the standart primitive shape
const TopoDS_Shape base = getBaseShape();
if(base.IsNull()) {
if(getAddSubType() == FeatureAddSub::Additive)
Shape.setValue(primitiveShape);
else
return new App::DocumentObjectExecReturn("Cannot subtract primitive feature without base feature");
AddSubShape.setValue(base);
return App::DocumentObject::StdReturn;
}
if(getAddSubType() == FeatureAddSub::Additive) {
BRepAlgoAPI_Fuse mkFuse(base, primitiveShape);
if (!mkFuse.IsDone())
return new App::DocumentObjectExecReturn("Adding the primitive failed");
// we have to get the solids (fuse sometimes creates compounds)
TopoDS_Shape boolOp = this->getSolid(mkFuse.Shape());
// lets check if the result is a solid
if (boolOp.IsNull())
return new App::DocumentObjectExecReturn("Resulting shape is not a solid");
Shape.setValue(boolOp);
AddSubShape.setValue(primitiveShape);
}
else if(getAddSubType() == FeatureAddSub::Subtractive) {
BRepAlgoAPI_Cut mkCut(base, primitiveShape);
if (!mkCut.IsDone())
return new App::DocumentObjectExecReturn("Subtracting the primitive failed");
// we have to get the solids (fuse sometimes creates compounds)
TopoDS_Shape boolOp = this->getSolid(mkCut.Shape());
// lets check if the result is a solid
if (boolOp.IsNull())
return new App::DocumentObjectExecReturn("Resulting shape is not a solid");
Shape.setValue(boolOp);
AddSubShape.setValue(primitiveShape);
}
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
return new App::DocumentObjectExecReturn(e->GetMessageString());
}
return App::DocumentObject::StdReturn;
}
TopLoc_Location FeaturePrimitive::calculateLocation()
{
return TopLoc_Location();
}
PROPERTY_SOURCE(PartDesign::Box, PartDesign::FeaturePrimitive)
Box::Box()
{
ADD_PROPERTY_TYPE(Length,(10.0f),"Box",App::Prop_None,"The length of the box");
ADD_PROPERTY_TYPE(Width ,(10.0f),"Box",App::Prop_None,"The width of the box");
ADD_PROPERTY_TYPE(Height,(10.0f),"Box",App::Prop_None,"The height of the box");
primitiveType = FeaturePrimitive::Box;
}
App::DocumentObjectExecReturn* Box::execute(void)
{
double L = Length.getValue();
double W = Width.getValue();
double H = Height.getValue();
if (L < Precision::Confusion())
return new App::DocumentObjectExecReturn("Length of box too small");
if (W < Precision::Confusion())
return new App::DocumentObjectExecReturn("Width of box too small");
if (H < Precision::Confusion())
return new App::DocumentObjectExecReturn("Height of box too small");
try {
// Build a box using the dimension attributes
BRepPrimAPI_MakeBox mkBox(L, W, H);
return FeaturePrimitive::execute(mkBox.Shape());
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
return new App::DocumentObjectExecReturn(e->GetMessageString());
}
}
short int Box::mustExecute() const
{
if ( Length.isTouched() ||
Height.isTouched() ||
Width.isTouched() )
return 1;
return FeaturePrimitive::mustExecute();
}
PROPERTY_SOURCE(PartDesign::AdditiveBox, PartDesign::Box)
PROPERTY_SOURCE(PartDesign::SubtractiveBox, PartDesign::Box)
}

View File

@@ -0,0 +1,105 @@
/***************************************************************************
* Copyright (c) 2015 Stefan Tröger <stefantroeger@gmx.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef PARTDESIGN_FeaturePrimitive_H
#define PARTDESIGN_FeaturePrimitive_H
#include <App/PropertyStandard.h>
#include <App/PropertyUnits.h>
#include "FeatureAddSub.h"
namespace PartDesign
{
class PartDesignExport FeaturePrimitive : public PartDesign::FeatureAddSub
{
PROPERTY_HEADER(PartDesign::FeaturePrimitive);
public:
enum Type {
Box=0,
Cylinder,
Sphere
};
FeaturePrimitive();
virtual const char* getViewProviderName(void) const {
return "getViewProviderPrimitive";
}
Type getPrimitiveType() {return primitiveType;};
/// The references defining the primtive base
App::PropertyLinkSubList References;
protected:
//make the boolean ops with the primitives provided by the derived features
App::DocumentObjectExecReturn* execute(const TopoDS_Shape& primitiveShape);
//calculate the position of the primitive from the support and the exis
TopLoc_Location calculateLocation();
Type primitiveType = Box;
};
class PartDesignExport Box : public PartDesign::FeaturePrimitive {
PROPERTY_HEADER(Part::Box);
public:
Box();
App::PropertyLength Length,Height,Width;
/** @name methods override feature */
//@{
/// recalculate the Feature
App::DocumentObjectExecReturn *execute(void);
short mustExecute() const;
protected:
};
class PartDesignExport AdditiveBox : public Box {
PROPERTY_HEADER(PartDesign::AdditiveBox);
AdditiveBox() {
addSubType = FeatureAddSub::Additive;
}
};
class PartDesignExport SubtractiveBox : public Box {
PROPERTY_HEADER(PartDesign::SubtractiveBox);
SubtractiveBox() {
addSubType = FeatureAddSub::Subtractive;
}
};
} //namespace PartDesign
#endif // PARTDESIGN_FeaturePrimitive_H