OpenLexocad  27.1
ProductImpl.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <App/Application.h>
4 #include <App/Product.h>
5 #include <Base/Exception.h>
6 #include <Core/Core_Interface.h>
11 #include <Draw/DrawStyle.h>
12 #include <Draw/Texture2.h>
13 #include <Draw/Texture2Transform.h>
14 #include <Geom/Ax1.h>
15 #include <Geom/Ax2.h>
16 #include <Geom/Bnd_Box.h>
17 #include <Geom/Dir.h>
18 #include <Geom/Trsf.h>
19 #include <Geom/Vec.h>
20 #include <OpenLxApp/Geometry.h>
21 #include <OpenLxApp/Object.h>
22 #include <OpenLxApp/Property.h>
23 #include <OpenLxApp/PropertySet.h>
24 #include <OpenLxApp/Value.h>
25 
26 #include <QFileInfo>
27 #include <memory>
28 
29 
30 namespace OpenLxApp
31 {
32 class Geometry;
33 
40 {
41 public:
42  ProductImpl(App::Product* aProduct);
43  ~ProductImpl(void);
44 
45  std::shared_ptr<Document> getDocument() const;
46 
47  virtual bool setGeometry(std::shared_ptr<Geometry> geo);
48  std::shared_ptr<Geometry> getGeometry() const;
51 
53  void setLocalPlacement(const Geom::Ax2& pos);
54  Geom::Trsf getTransform() const;
56  void setTransform(const Geom::Trsf& t);
57  void translate(const Geom::Vec& aVec, Geom::CoordSpace aCoordSpace = Geom::CoordSpace::WCS);
58  void rotate(const Geom::Ax1& axis, double angle, Geom::CoordSpace aCoordSpace = Geom::CoordSpace::WCS);
59  bool getLocalAxes(Geom::Ax2& localAxes);
60  void setLocalAxes(const Geom::Dir& zHeight, const Geom::Dir& xLength);
61 
62  pConstShape getShape() const;
63  pConstShape getLocalShape() const;
64 
65  void setOglMaterial(const Draw::OglMaterial& mat, int faceIndex = -1);
66  Draw::OglMaterial getOglMaterial() const;
67 
68  void setAmbientColor(const Base::Color& aCol);
69  void setDiffuseColor(const Base::Color& aCol);
70  void setSpecularColor(const Base::Color& aCol);
71  void setEmissiveColor(const Base::Color& aCol);
72  void setReflectiveColor(const Base::Color& aCol);
73  void setShininess(int aVal);
74  void setTransparency(int aVal);
75 
76  void setDrawStyle(const Draw::DrawStyle& ds);
77  Draw::DrawStyle getDrawStyle() const;
78  void setLineWidth(float width);
79  void setTexture(const Draw::Texture2& tex, int faceIndex = -1);
80  void setLengthAndCrossTexture(const Draw::Texture2& lengthTexture,
81  const Draw::Texture2& crossTexture,
82  const Draw::Texture2Transform& lengthTextureTrsf = Draw::Texture2Transform(),
83  const Draw::Texture2Transform& crossTextureTrsf = Draw::Texture2Transform());
84 
85  void setPositionNb(long value);
86  long getPositionNb() const;
87 
88  bool isVisible() const;
89  void setVisible(bool onoff);
90 
91  std::shared_ptr<PropertyInteger> registerPropertyInteger(const std::string& aName,
92  int aDefaultValue,
95  int aTranslationId = -1);
96  std::shared_ptr<PropertyEnum> registerPropertyEnum(const std::string& aName,
97  int aDefaultValue,
100  int aTranslationId = -1);
101 
102  std::shared_ptr<PropertyDouble> registerPropertyDouble(const std::string& aName,
103  double aDefaultValue,
106  int aTranslationId = -1);
107 
108  std::shared_ptr<PropertyButton> registerPropertyButton(const std::string& aName,
111  int aTranslationId = -1);
112 
113  std::shared_ptr<PropertyBool> registerPropertyBool(const std::string& aName,
114  bool aDefaultValue,
117  int aTranslationId = -1);
118 
119  std::shared_ptr<PropertyString> registerPropertyString(const std::string& aName,
120  const Base::String& aDefaultValue,
123  int aTranslationId = -1);
124 
125  std::shared_ptr<PropertyColor> registerPropertyColor(const std::string& aName,
126  const Base::Color& aDefaultValue,
129  int aTranslationId = -1);
130 
131  std::shared_ptr<OpenLxApp::Property> getProperty(const std::string& aName) const;
132  std::map<std::string, std::shared_ptr<Property>> getPropertyMap() const;
133  std::shared_ptr<PropertyInteger> getPropertyInteger(const std::string& aName) const;
134  std::shared_ptr<PropertyEnum> getPropertyEnum(const std::string& aName) const;
135  std::shared_ptr<PropertyDouble> getPropertyDouble(const std::string& aName) const;
136  std::shared_ptr<PropertyButton> getPropertyButton(const std::string& aName) const;
137  std::shared_ptr<PropertyBool> getPropertyBool(const std::string& aName) const;
138  std::shared_ptr<PropertyString> getPropertyString(const std::string& aName) const;
139  std::shared_ptr<PropertyColor> getPropertyColor(const std::string& aName) const;
140  bool isRegisteredProperty(const std::string& aName) const;
141 
142 
143  void translateAfterScaled(const Geom::Vec& aVec, const Geom::Pnt& aScaleBasePnt);
145  void setPropertyHeader(const Base::String& aDefaultName, int aTranslationId);
146  void setPropertyGroupName(const Base::String& aDefaultName, int aTranslationId);
149 
150  // User defined properties
151  std::shared_ptr<PropertyUser> getPropertyUser(const std::string& aName) const;
152  std::vector<std::shared_ptr<PropertyUser>> getPropertyUser() const;
153  std::vector<Base::String> getPropertySetNames() const;
154  std::shared_ptr<PropertySet> getPropertySetByName(const Base::String& aName) const;
155 
169  template <typename T>
170  bool registerPythonClass(std::shared_ptr<T> aObject, const std::string& aClassName, const std::string& aParentClassName)
171  {
172  if (aClassName == "" || aParentClassName == "")
173  return false;
174 
175  auto pco = mProduct->getPythonClassObject();
176  if (pco)
177  {
178  std::cout << "Already registered!" << std::endl;
179  return true; // Class is already registered.
180  }
181  else
182  {
184 
185  // Get Script
187  if (scriptId.isNull())
188  {
189  Base::Message().showMessageBoxError(App::GetApplication().getQApplicationName(),
190  QString("Cannot register Python Script. \nGlobalId = %1\nDid you forget to "
191  "add\n\ndoc.registerPythonScript(Base.GlobalId(\"{<aGloballyUniqueId>}\") \n\nto your "
192  "Lexocad Python Script?\nSee https://www.guidgenerator.com to generate a GlobalId.")
193  .arg(QString::fromStdWString(scriptId.toString().c_str())));
194  return false;
195  }
196 
198  if (!pythonScriptObject)
199  {
201  App::GetApplication().getQApplicationName(),
202  QString("Cannot get the registered Script.\nGlobalId = %1").arg(QString::fromStdWString(scriptId.toString().c_str())));
203  return false;
204  }
205 
206  // Check if class is already registered...
207  auto classId = aObject->getGlobalClassId();
208  if (classId.isNull())
209  {
210  Base::Message().showMessageBoxError(App::GetApplication().getQApplicationName(),
211  QString("Cannot register Python class. \nGlobalId = %1\nDid you forget to add\n\ndef "
212  "getGlobalClassId(self):\n return Base.GlobalId(\"{<aGloballyUniqueId>}\") \n\nto "
213  "your Lexocad Python class?\nSee https://www.guidgenerator.com to generate a GlobalId.")
214  .arg(QString::fromStdWString(classId.toString().c_str())));
215  return false;
216  }
217 
218  // Check if the file base name contains whitespaces.
219  // This is not allowed because it will later cause
220  // problems when trying to import the module.
221  // eg: 'import My module name' will cause an error.
222  QFileInfo fi(QString::fromStdWString(fileName.c_str()));
223  QString baseName = fi.completeBaseName();
224  if (baseName.contains(' '))
225  {
226  Base::Message().showMessageBoxError(QString::fromStdWString(Core::CoreApplication::instance()->getApplicationName().c_str()),
227  QString("Cannot import '%1'.\nWhitespaces in names are not allowed.").arg(baseName));
228  return false;
229  }
230 
231  Base::String thisClassName = Base::StringTool::toString(aClassName);
232  Base::String superClassName = Base::StringTool::toString(aParentClassName);
233 
234  if (auto regClass = Core::PythonClassObject::getRegisteredClass(classId))
235  {
236  if (regClass->className.getValue() != thisClassName)
237  {
239  App::GetApplication().getQApplicationName(),
240  QString("Cannot register Python class. \nThe GlobalId is already registered with a different class (%1)")
241  .arg(QString::fromStdWString(regClass->className.getValue().c_str())));
242  return false;
243  }
244  mProduct->setPythonClassObject(regClass);
245  return true; // Class is already registered.
246  }
247  else
248  {
249  std::cout << "First register" << std::endl;
250  Core::CoreDocument* doc = mProduct->getDocument();
251  auto pythonClassObject = doc->createObject<Core::PythonClassObject>();
252  mProduct->setPythonClassObject(pythonClassObject);
253  pythonClassObject->className.setValue(thisClassName);
254  pythonClassObject->classId.setValue(classId);
255  pythonClassObject->superClassName.setValue(superClassName);
256  Core::PythonClassObject::registerClass(pythonClassObject);
257 
258  pythonClassObject->pythonScriptObject.setValue(pythonScriptObject);
259  pythonClassObject->standardManipulatorPolicy.setValue(
264  return true; // Class is registered successfully.
265  }
266  }
267  }
268 
269  template <typename T>
270  void onChange(std::shared_ptr<T> aObject, Core::DocObject* aCaller, const Core::DocObjectObserverMsg& aReason)
271  {
272  if (getDocument()->isEditing())
273  return;
274 
275  switch (aReason.msgId)
276  {
278  aObject->onPropertyChanged(Base::StringTool::toStlString(aReason.value1.toString()));
279  break;
281  {
282  const auto& vec = aReason.value1.toVector();
283  const auto& pnt = aReason.value2.toPoint();
284 
285  if (vec.magnitude() > Geom::Precision::linear_Resolution())
286  {
287  aObject->onScaling(vec, pnt);
288  }
289  break;
290  }
291  default:
292  break;
293  }
294  }
295 
296 private:
297  App::Product* mProduct = nullptr;
298 };
299 
300 } // namespace OpenLxApp
void setStandardManipulatorPolicy(const Core::StandardManipulatorPolicy &aPolicy)
Geom::Trsf getGeometryToWorldTransform() const
void setReflectiveColor(const Base::Color &aCol)
Definition: StandardManipulatorPolicy.h:14
std::shared_ptr< PropertyInteger > getPropertyInteger(const std::string &aName) const
Core::Variant value1
Definition: DocObjectObserver.h:19
void translateAfterScaled(const Geom::Vec &aVec, const Geom::Pnt &aScaleBasePnt)
Definition: StandardManipulatorPolicy.h:26
std::shared_ptr< PropertyBool > registerPropertyBool(const std::string &aName, bool aDefaultValue, Property::Visible aVisible=Property::VISIBLE, Property::Editable aEditable=Property::EDITABLE, int aTranslationId=-1)
Base::String toString(bool *ok=0) const
Definition: Color.h:45
void setAmbientColor(const Base::Color &aCol)
Definition: Property.h:38
bool getLocalAxes(Geom::Ax2 &localAxes)
Defines a non-persistent 3D Cartesian point.
Definition: Pnt.h:43
Editable
Definition: Property.h:35
LX_CORE_EXPORT Base::GlobalId getCurrentScriptId()
Definition: StandardManipulatorPolicy.h:24
void setDrawStyle(const Draw::DrawStyle &ds)
std::shared_ptr< Geometry > getGeometry() const
void setLocalPlacement(const Geom::Ax2 &pos)
Visible
Definition: Property.h:29
std::shared_ptr< PropertyString > registerPropertyString(const std::string &aName, const Base::String &aDefaultValue, Property::Visible aVisible=Property::VISIBLE, Property::Editable aEditable=Property::EDITABLE, int aTranslationId=-1)
static Base::String toString(const T &t)
Definition: StringTool.h:78
static bool registerClass(PythonClassObject *aClassObject)
Definition: Property.h:32
Definition: StandardManipulatorPolicy.h:22
static const double linear_Resolution()
Definition: Precision.h:24
void setOglMaterial(const Draw::OglMaterial &mat, int faceIndex=-1)
ProductImpl implements the Product behavior of an object. This is used in Product and SubElement.
Definition: ProductImpl.h:39
static PythonClassObject * getRegisteredClass(const Base::GlobalId &aClassId)
bool isRegisteredProperty(const std::string &aName) const
Definition: StandardManipulatorPolicy.h:23
pConstShape getLocalShape() const
Definition: Bnd_Box.h:63
bool registerPythonClass(std::shared_ptr< T > aObject, const std::string &aClassName, const std::string &aParentClassName)
Definition: ProductImpl.h:170
Base::String getPropertyHeader() const
A Utf-16 (windows) or ucs4 (unix) encoded string class.
Definition: String.h:23
std::shared_ptr< PropertyEnum > registerPropertyEnum(const std::string &aName, int aDefaultValue, Property::Visible aVisible=Property::VISIBLE, Property::Editable aEditable=Property::EDITABLE, int aTranslationId=-1)
std::vector< Base::String > getPropertySetNames() const
void setSpecularColor(const Base::Color &aCol)
pConstShape getShape() const
std::shared_ptr< PropertySet > getPropertySetByName(const Base::String &aName) const
Draw::DrawStyle getDrawStyle() const
LX_CORE_EXPORT Base::String getCurrentScriptFilePath()
Definition: CoreDocument.h:210
std::shared_ptr< PropertyEnum > getPropertyEnum(const std::string &aName) const
std::shared_ptr< PropertyDouble > getPropertyDouble(const std::string &aName) const
std::shared_ptr< PropertyDouble > registerPropertyDouble(const std::string &aName, double aDefaultValue, Property::Visible aVisible=Property::VISIBLE, Property::Editable aEditable=Property::EDITABLE, int aTranslationId=-1)
void translate(const Geom::Vec &aVec, Geom::CoordSpace aCoordSpace=Geom::CoordSpace::WCS)
void setVisible(bool onoff)
Definition: StandardManipulatorPolicy.h:25
void setPropertyHeader(const Base::String &aDefaultName, int aTranslationId)
void onChange(std::shared_ptr< T > aObject, Core::DocObject *aCaller, const Core::DocObjectObserverMsg &aReason)
Definition: ProductImpl.h:270
Definition: Ax2.h:67
const wchar_t * c_str() const
Returns wchar_t array.
Definition: String.h:68
Definition: Trsf.h:57
Definition: Dir.h:45
Definition: GlobalId.h:29
Definition: ActiveScript.h:7
static PythonScriptObject * getRegisteredScript(const Base::GlobalId &aScriptPath)
LX_BASE_EXPORT Base::MessageBase & Message(void)
Definition: Message.h:161
Definition: StandardManipulatorPolicy.h:9
Definition: PythonScriptObject.h:10
Geom::Vec toVector(bool *ok=0) const
std::shared_ptr< PropertyButton > getPropertyButton(const std::string &aName) const
Definition: StandardManipulatorPolicy.h:13
Keeps the information about a Python class like class name, the super class etc. Also keeps the infor...
Definition: PythonClassObject.h:24
std::vector< std::shared_ptr< PropertyUser > > getPropertyUser() const
bool isNull() const
Geom::Ax2 getLocalPlacement() const
Draw::OglMaterial getOglMaterial() const
Base::String toString() const
std::shared_ptr< PropertyColor > registerPropertyColor(const std::string &aName, const Base::Color &aDefaultValue, Property::Visible aVisible=Property::VISIBLE, Property::Editable aEditable=Property::EDITABLE, int aTranslationId=-1)
virtual void showMessageBoxError(const QString &title, const QString &text, QWidget *parent=nullptr, bool silent=false)
static std::string toStlString(const T &t)
Definition: StringTool.h:30
void setLengthAndCrossTexture(const Draw::Texture2 &lengthTexture, const Draw::Texture2 &crossTexture, const Draw::Texture2Transform &lengthTextureTrsf=Draw::Texture2Transform(), const Draw::Texture2Transform &crossTextureTrsf=Draw::Texture2Transform())
Geom::Trsf getTransform() const
void setShininess(int aVal)
Definition: Ax1.h:50
ProductImpl(App::Product *aProduct)
Defines a non-persistent vector in 3D space.
Definition: Vec.h:44
Definition: StandardManipulatorPolicy.h:27
std::shared_ptr< PropertyColor > getPropertyColor(const std::string &aName) const
void setDiffuseColor(const Base::Color &aCol)
Geom::Pnt toPoint(bool *ok=0) const
void setTransparency(int aVal)
MessageId msgId
Definition: DocObjectObserver.h:18
void setPositionNb(long value)
std::shared_ptr< PropertyButton > registerPropertyButton(const std::string &aName, Property::Visible aVisible=Property::VISIBLE, Property::Editable aEditable=Property::EDITABLE, int aTranslationId=-1)
void setTexture(const Draw::Texture2 &tex, int faceIndex=-1)
virtual bool setGeometry(std::shared_ptr< Geometry > geo)
void setLineWidth(float width)
std::shared_ptr< PropertyInteger > registerPropertyInteger(const std::string &aName, int aDefaultValue, Property::Visible aVisible=Property::VISIBLE, Property::Editable aEditable=Property::EDITABLE, int aTranslationId=-1)
std::shared_ptr< Document > getDocument() const
std::shared_ptr< Topo::Shape const > pConstShape
Definition: Variant.h:77
bool isVisible() const
void setPropertyGroupName(const Base::String &aDefaultName, int aTranslationId)
Geom::Trsf getLocalToWorldTransform() const
void rotate(const Geom::Ax1 &axis, double angle, Geom::CoordSpace aCoordSpace=Geom::CoordSpace::WCS)
static CoreApplication * instance(void)
Returns the Singleton.
void setLocalAxes(const Geom::Dir &zHeight, const Geom::Dir &xLength)
void setTransform(const Geom::Trsf &t)
Base::String fileName
Definition: CoreDocument.h:186
long getPositionNb() const
std::shared_ptr< PropertyString > getPropertyString(const std::string &aName) const
void setEmissiveColor(const Base::Color &aCol)
Base::String getPropertyGroupName() const
Core::Variant value2
Definition: DocObjectObserver.h:20
Definition: DocObject.h:51
Geom::Bnd_Box getBoundingBox() const
std::shared_ptr< PropertyBool > getPropertyBool(const std::string &aName) const
CoordSpace
Definition: GeomEnums.h:5
Definition: DocObjectObserver.h:9
std::map< std::string, std::shared_ptr< Property > > getPropertyMap() const
std::shared_ptr< OpenLxApp::Property > getProperty(const std::string &aName) const