/*************************************************************************** * Copyright (c) Jürgen Riegel (juergen.riegel@web.de) 2002 * * * * 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_ # include # include #endif #include #include #include "FeaturePartBox.h" using namespace Part; PROPERTY_SOURCE(Part::Box, Part::Primitive) 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"); } short Box::mustExecute() const { if (Length.isTouched() || Height.isTouched() || Width.isTouched() ) return 1; return Primitive::mustExecute(); } 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); TopoDS_Shape ResultShape = mkBox.Shape(); this->Shape.setValue(ResultShape); } catch (Standard_Failure) { Handle_Standard_Failure e = Standard_Failure::Caught(); return new App::DocumentObjectExecReturn(e->GetMessageString()); } return App::DocumentObject::StdReturn; } /** * This method was added for backward-compatibility. In former versions * of Box we had the properties x,y,z and l,h,w which have changed to * Location -- as replacement for x,y and z and Length, Height and Width. */ void Box::Restore(Base::XMLReader &reader) { reader.readElement("Properties"); int Cnt = reader.getAttributeAsInteger("Count"); bool location_xyz = false; bool location_axis = false; bool distance_lhw = false; Base::Placement plm; App::PropertyDistance x,y,z; App::PropertyDistance l,w,h; App::PropertyVector Axis, Location; Axis.setValue(0.0f,0.0f,1.0f); for (int i=0 ;igetTypeId().getName(), tn.c_str()) == 0) prop->Restore(reader); reader.readEndElement("Property"); } if (distance_lhw) { this->Length.setValue(l.getValue()); this->Height.setValue(h.getValue()); this->Width.setValue(w.getValue()); } // for 0.7 releases or earlier if (location_xyz) { plm.setPosition(Base::Vector3d(x.getValue(),y.getValue(),z.getValue())); this->Placement.setValue(this->Placement.getValue() * plm); this->Shape.StatusBits.set(10); // override the shape's location later on } // for 0.8 releases else if (location_axis) { Base::Vector3f d = Axis.getValue(); Base::Vector3f p = Location.getValue(); Base::Rotation rot(Base::Vector3d(0.0,0.0,1.0), Base::Vector3d(d.x,d.y,d.z)); plm.setRotation(rot); plm.setPosition(Base::Vector3d(p.x,p.y,p.z)); this->Placement.setValue(this->Placement.getValue() * plm); this->Shape.StatusBits.set(10); // override the shape's location later on } reader.readEndElement("Properties"); } void Box::onChanged(const App::Property* prop) { if (prop == &Length || prop == &Width || prop == &Height) { if (!isRestoring()) { App::DocumentObjectExecReturn *ret = recompute(); delete ret; } } else if (prop == &this->Shape) { // see Box::Restore if (this->Shape.StatusBits.test(10)) { this->Shape.StatusBits.reset(10); App::DocumentObjectExecReturn *ret = recompute(); delete ret; return; } } Part::Primitive::onChanged(prop); }