From 3753d47cbd74a3f73543dc0ac8f4a026dd83d13e Mon Sep 17 00:00:00 2001 From: jfa Date: Wed, 24 Jun 2020 17:02:58 +0300 Subject: [PATCH] Sketcher Offset: API --- src/SketchAPI/CMakeLists.txt | 2 + src/SketchAPI/SketchAPI.i | 2 + src/SketchAPI/SketchAPI_Offset.cpp | 108 +++++++++++++++++++++++ src/SketchAPI/SketchAPI_Offset.h | 90 +++++++++++++++++++ src/SketchAPI/SketchAPI_Sketch.cpp | 14 +++ src/SketchAPI/SketchAPI_Sketch.h | 9 ++ src/SketchAPI/SketchAPI_swig.h | 1 + src/SketchPlugin/SketchPlugin_Offset.cpp | 49 ++++++++-- src/SketchPlugin/SketchPlugin_Offset.h | 15 +++- 9 files changed, 279 insertions(+), 11 deletions(-) create mode 100644 src/SketchAPI/SketchAPI_Offset.cpp create mode 100644 src/SketchAPI/SketchAPI_Offset.h diff --git a/src/SketchAPI/CMakeLists.txt b/src/SketchAPI/CMakeLists.txt index c81d857be..3b5b81347 100644 --- a/src/SketchAPI/CMakeLists.txt +++ b/src/SketchAPI/CMakeLists.txt @@ -35,6 +35,7 @@ SET(PROJECT_HEADERS SketchAPI_MacroEllipse.h SketchAPI_MacroEllipticArc.h SketchAPI_Mirror.h + SketchAPI_Offset.h SketchAPI_Point.h SketchAPI_Projection.h SketchAPI_Rectangle.h @@ -59,6 +60,7 @@ SET(PROJECT_SOURCES SketchAPI_MacroEllipse.cpp SketchAPI_MacroEllipticArc.cpp SketchAPI_Mirror.cpp + SketchAPI_Offset.cpp SketchAPI_Point.cpp SketchAPI_Projection.cpp SketchAPI_Rectangle.cpp diff --git a/src/SketchAPI/SketchAPI.i b/src/SketchAPI/SketchAPI.i index ba30a72da..2ee3108cd 100644 --- a/src/SketchAPI/SketchAPI.i +++ b/src/SketchAPI/SketchAPI.i @@ -71,6 +71,7 @@ %shared_ptr(SketchAPI_IntersectionPoint) %shared_ptr(SketchAPI_Line) %shared_ptr(SketchAPI_Mirror) +%shared_ptr(SketchAPI_Offset) %shared_ptr(SketchAPI_Sketch) %shared_ptr(SketchAPI_SketchEntity) %shared_ptr(SketchAPI_Point) @@ -579,6 +580,7 @@ %include "SketchAPI_BSpline.h" %include "SketchAPI_Projection.h" %include "SketchAPI_Mirror.h" +%include "SketchAPI_Offset.h" %include "SketchAPI_Translation.h" %include "SketchAPI_Rectangle.h" %include "SketchAPI_Rotation.h" diff --git a/src/SketchAPI/SketchAPI_Offset.cpp b/src/SketchAPI/SketchAPI_Offset.cpp new file mode 100644 index 000000000..6d4f24e4e --- /dev/null +++ b/src/SketchAPI/SketchAPI_Offset.cpp @@ -0,0 +1,108 @@ +// Copyright (C) 2014-2019 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "SketchAPI_Offset.h" +#include +//-------------------------------------------------------------------------------------- +#include +#include +#include + +//-------------------------------------------------------------------------------------- +SketchAPI_Offset::SketchAPI_Offset (const std::shared_ptr & theFeature) + : ModelHighAPI_Interface(theFeature) +{ + initialize(); +} + +SketchAPI_Offset::SketchAPI_Offset (const std::shared_ptr & theFeature, + const std::list > & theObjects, + const ModelHighAPI_Double & theOffsetValue, + bool theIsReversed, + bool theIsAuxiliary) + : ModelHighAPI_Interface(theFeature) +{ + if (initialize()) { + fillAttribute(theObjects, edgesList()); + fillAttribute(theOffsetValue, value()); + fillAttribute(theIsReversed, reversed()); + fillAttribute(theIsAuxiliary, auxiliary()); + + execute(); + } +} + +SketchAPI_Offset::~SketchAPI_Offset() +{ +} + +std::list > SketchAPI_Offset::offset() const +{ + std::list aList = createdList()->list(); + std::list anIntermediate; + std::list::const_iterator anIt = aList.begin(); + for (; anIt != aList.end(); ++anIt) { + FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt); + anIntermediate.push_back(aFeature); + } + return SketchAPI_SketchEntity::wrap(anIntermediate); +} + +//-------------------------------------------------------------------------------------- + +void SketchAPI_Offset::dump (ModelHighAPI_Dumper& theDumper) const +{ + FeaturePtr aBase = feature(); + const std::string& aSketchName = theDumper.parentName(aBase); + + AttributeRefListPtr aOffsetObjects = edgesList(); + AttributeDoublePtr aValue = value(); + AttributeBooleanPtr aReversed = reversed(); + AttributeBooleanPtr anAux = auxiliary(); + + // Check all attributes are already dumped. If not, store the feature as postponed. + if (!theDumper.isDumped(aOffsetObjects)) { + theDumper.postpone(aBase); + return; + } + + theDumper << aBase << " = " << aSketchName << ".addOffset(" << aOffsetObjects << ", " + << aValue << ", " << aReversed << ", " << anAux << ")" << std::endl; + + // Dump variables for a list of created features + theDumper << "["; + std::list > aList = offset(); + std::list >::const_iterator anIt = aList.begin(); + for (; anIt != aList.end(); ++anIt) { + if (anIt != aList.begin()) + theDumper << ", "; + theDumper << (*anIt)->feature(); + } + theDumper << "] = " << theDumper.name(aBase) << ".offset()" << std::endl; + + // Set necessary "auxiliary" flag for created features + // (flag is set if it differs to anAux) + for (anIt = aList.begin(); anIt != aList.end(); ++anIt) { + FeaturePtr aFeature = (*anIt)->feature(); + bool aFeatAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value(); + if (aFeatAux != anAux->value()) + theDumper << theDumper.name((*anIt)->feature(), false) + << ".setAuxiliary(" << aFeatAux << ")" < + +#include + +#include +#include +//-------------------------------------------------------------------------------------- +class ModelAPI_Object; +class ModelHighAPI_RefAttr; +class ModelHighAPI_Double; +//-------------------------------------------------------------------------------------- +/**\class SketchAPI_Offset + * \ingroup CPPHighAPI + * \brief Interface for Offset feature + */ +class SketchAPI_Offset : public ModelHighAPI_Interface +{ +public: + /// Constructor without values + SKETCHAPI_EXPORT + explicit SketchAPI_Offset(const std::shared_ptr & theFeature); + /// Constructor with values + SKETCHAPI_EXPORT + SketchAPI_Offset(const std::shared_ptr & theFeature, + const std::list > & theObjects, + const ModelHighAPI_Double & theOffsetValue, + bool theIsReversed, + bool theIsAuxiliary); + /// Destructor + SKETCHAPI_EXPORT + virtual ~SketchAPI_Offset(); + + INTERFACE_5(SketchPlugin_Offset::ID(), + + edgesList, SketchPlugin_Offset::EDGES_ID(), + ModelAPI_AttributeRefList, /** Offset edges list */, + + value, SketchPlugin_Offset::VALUE_ID(), + ModelAPI_AttributeDouble, /** Value */, + + reversed, SketchPlugin_Offset::REVERSED_ID(), + ModelAPI_AttributeBoolean, /** Negative value */, + + auxiliary, SketchPlugin_Offset::AUXILIARY_ID(), + ModelAPI_AttributeBoolean, /** Auxiliary */, + + createdList, SketchPlugin_Offset::CREATED_ID(), + ModelAPI_AttributeRefList, /** Created edges list */ + ) + + /// List of created objects + SKETCHAPI_EXPORT + std::list > offset() const; + + /// Dump wrapped feature + virtual void dump(ModelHighAPI_Dumper& theDumper) const; +}; + +//! Pointer on Offset object +typedef std::shared_ptr OffsetPtr; + +//-------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------- +#endif /* SRC_SKETCHAPI_SKETCHAPI_OFFSET_H_ */ diff --git a/src/SketchAPI/SketchAPI_Sketch.cpp b/src/SketchAPI/SketchAPI_Sketch.cpp index c713dfd97..2046d5103 100644 --- a/src/SketchAPI/SketchAPI_Sketch.cpp +++ b/src/SketchAPI/SketchAPI_Sketch.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include //-------------------------------------------------------------------------------------- #include @@ -67,6 +68,7 @@ #include "SketchAPI_MacroEllipse.h" #include "SketchAPI_MacroEllipticArc.h" #include "SketchAPI_Mirror.h" +#include "SketchAPI_Offset.h" #include "SketchAPI_Point.h" #include "SketchAPI_Projection.h" #include "SketchAPI_Rectangle.h" @@ -889,6 +891,18 @@ std::shared_ptr SketchAPI_Sketch::addMirror( return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects)); } +//-------------------------------------------------------------------------------------- +std::shared_ptr SketchAPI_Sketch::addOffset( + const std::list > & theObjects, + const ModelHighAPI_Double & theValue, + const bool theReversed, + const bool theAuxiliary) +{ + std::shared_ptr aFeature = + compositeFeature()->addFeature(SketchPlugin_Offset::ID()); + return OffsetPtr(new SketchAPI_Offset(aFeature, theObjects, theValue, theReversed, theAuxiliary)); +} + //-------------------------------------------------------------------------------------- std::shared_ptr SketchAPI_Sketch::addTranslation( const std::list > & theObjects, diff --git a/src/SketchAPI/SketchAPI_Sketch.h b/src/SketchAPI/SketchAPI_Sketch.h index 30674f4fb..6174ca23f 100644 --- a/src/SketchAPI/SketchAPI_Sketch.h +++ b/src/SketchAPI/SketchAPI_Sketch.h @@ -50,6 +50,7 @@ class SketchAPI_BSpline; class SketchAPI_IntersectionPoint; class SketchAPI_Line; class SketchAPI_Mirror; +class SketchAPI_Offset; class SketchAPI_Point; class SketchAPI_Projection; class SketchAPI_Rectangle; @@ -370,6 +371,14 @@ public: const ModelHighAPI_RefAttr & theMirrorLine, const std::list > & theObjects); + /// Add offset + SKETCHAPI_EXPORT + std::shared_ptr addOffset( + const std::list > & theObjects, + const ModelHighAPI_Double & theValue, + const bool theReversed, + const bool theAuxiliary); + /// Add translation SKETCHAPI_EXPORT std::shared_ptr addTranslation( diff --git a/src/SketchAPI/SketchAPI_swig.h b/src/SketchAPI/SketchAPI_swig.h index 28370b70c..d166f4e51 100644 --- a/src/SketchAPI/SketchAPI_swig.h +++ b/src/SketchAPI/SketchAPI_swig.h @@ -37,6 +37,7 @@ #include "SketchAPI_IntersectionPoint.h" #include "SketchAPI_Line.h" #include "SketchAPI_Mirror.h" + #include "SketchAPI_Offset.h" #include "SketchAPI_Sketch.h" #include "SketchAPI_SketchEntity.h" #include "SketchAPI_Point.h" diff --git a/src/SketchPlugin/SketchPlugin_Offset.cpp b/src/SketchPlugin/SketchPlugin_Offset.cpp index 6ff214858..a9ccb1c97 100644 --- a/src/SketchPlugin/SketchPlugin_Offset.cpp +++ b/src/SketchPlugin/SketchPlugin_Offset.cpp @@ -39,8 +39,10 @@ #include #include #include +#include #include #include +#include #include #include @@ -66,12 +68,14 @@ void SketchPlugin_Offset::initDerivedClassAttributes() data()->addAttribute(EDGES_ID(), ModelAPI_AttributeRefList::typeId()); data()->addAttribute(VALUE_ID(), ModelAPI_AttributeDouble::typeId()); data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId()); + data()->addAttribute(CREATED_ID(), ModelAPI_AttributeRefList::typeId()); + + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CREATED_ID()); } void SketchPlugin_Offset::execute() { - ModelAPI_Tools::removeFeaturesAndReferences(myCreatedFeatures); - myCreatedFeatures.clear(); + removeCreated(); // remove created objects SketchPlugin_Sketch* aSketch = sketch(); if (!aSketch) return; @@ -105,6 +109,12 @@ void SketchPlugin_Offset::execute() } } + // Wait all objects being created, then send update events + static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED); + bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent); + if (isUpdateFlushed) + Events_Loop::loop()->setFlushed(anUpdateEvent, false); + // 5. Gather wires and make offset for each wire for (anEdgesIt = anEdgesList.begin(); anEdgesIt != anEdgesList.end(); anEdgesIt++) { FeaturePtr aFeature = ModelAPI_Feature::feature(*anEdgesIt); @@ -146,10 +156,14 @@ void SketchPlugin_Offset::execute() // 5.e. Store offset results. // Create sketch feature for each edge of anOffsetShape, and also store - // created features in myCreatedFeatures to remove them on next execute() + // created features in CREATED_ID() to remove them on next execute() addToSketch(anOffsetShape); } } + + // send events to update the sub-features by the solver + if (isUpdateFlushed) + Events_Loop::loop()->setFlushed(anUpdateEvent, true); } bool SketchPlugin_Offset::findWireOneWay (const FeaturePtr& theFirstEdge, @@ -255,7 +269,10 @@ bool SketchPlugin_Offset::findWireOneWay (const FeaturePtr& theFirstEdge, void SketchPlugin_Offset::addToSketch(const std::shared_ptr& anOffsetShape) { - //GeomAPI_ShapeExplorer::GeomAPI_ShapeExplorer + AttributeRefListPtr aRefListOfCreated = + std::dynamic_pointer_cast + (data()->attribute(SketchPlugin_Offset::CREATED_ID())); + ListOfShape aResEdges = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(anOffsetShape); std::list::const_iterator aResEdgesIt = aResEdges.begin(); for (; aResEdgesIt != aResEdges.end(); aResEdgesIt++) { @@ -352,7 +369,7 @@ void SketchPlugin_Offset::addToSketch(const std::shared_ptr& anOf } if (aResFeature.get()) { - myCreatedFeatures.insert(aResFeature); + aRefListOfCreated->append(aResFeature); aResFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue (boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()); @@ -423,8 +440,26 @@ void SketchPlugin_Offset::mkBSpline (FeaturePtr& theResult, void SketchPlugin_Offset::attributeChanged(const std::string& theID) { - ModelAPI_Tools::removeFeaturesAndReferences(myCreatedFeatures); - myCreatedFeatures.clear(); + removeCreated(); +} + +void SketchPlugin_Offset::removeCreated() +{ + if (!sketch()) return; + + // Remove all created objects + AttributeRefListPtr aRefListOfCreated = + std::dynamic_pointer_cast + (data()->attribute(SketchPlugin_Offset::CREATED_ID())); + std::list aList = aRefListOfCreated->list(); + std::set aSet; + std::list::iterator anIter = aList.begin(); + for (; anIter != aList.end(); anIter++) { + FeaturePtr aFeature = ModelAPI_Feature::feature(*anIter); + aSet.insert(aFeature); + } + ModelAPI_Tools::removeFeaturesAndReferences(aSet); + aRefListOfCreated->clear(); } bool SketchPlugin_Offset::customAction(const std::string& theActionId) diff --git a/src/SketchPlugin/SketchPlugin_Offset.h b/src/SketchPlugin/SketchPlugin_Offset.h index f8a86036d..482a75245 100644 --- a/src/SketchPlugin/SketchPlugin_Offset.h +++ b/src/SketchPlugin/SketchPlugin_Offset.h @@ -70,6 +70,13 @@ public: return ID; } + /// attribute to store the created objects + inline static const std::string& CREATED_ID() + { + static const std::string ID("created"); + return ID; + } + /// name for add wire action inline static const std::string& ADD_WIRE_ACTION_ID() { @@ -112,9 +119,12 @@ private: bool findWires(); // Create sketch feature for each edge of theOffsetResult, - // and store it in myCreatedFeatures to remove on next execute() + // and store it in CREATED_ID() void addToSketch (const std::shared_ptr& theOffsetResult); + // Remove created features + void removeCreated (); + // Create BSpline or BSplinePeriodic sketch feature from theEdge void mkBSpline (FeaturePtr& theResult, const GeomEdgePtr& theEdge); @@ -133,9 +143,6 @@ private: std::set& theEdgesSet, std::list& theChain, const bool isPrepend = false); - - // tmp - std::set myCreatedFeatures; }; #endif -- 2.39.2