Skip to content

Commit

Permalink
WIP5
Browse files Browse the repository at this point in the history
  • Loading branch information
HuguesDelorme committed Jun 6, 2024
1 parent e8a9f03 commit 6c07101
Show file tree
Hide file tree
Showing 13 changed files with 671 additions and 126 deletions.
75 changes: 3 additions & 72 deletions src/app/commands_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

#include <QtWidgets/QWidget>

#include "../qtscripting/script_application.h"
#include "../qtscripting/script_document.h"
#include "../qtscripting/script_global.h"
#include <QtCore/QFile>
#include <QtCore/QtDebug>
#include <QtQml/QJSEngine>
Expand Down Expand Up @@ -139,76 +138,8 @@ void CommandExecScript::execute()
};

if (!m_jsEngine) {
m_jsEngine = new QJSEngine(this);
m_jsEngine->installExtensions(QJSEngine::ConsoleExtension);

auto jsApp = new ScriptApplication(this->context()->guiApp()->application(), m_jsEngine);
QJSValue scriptApp = m_jsEngine->newQObject(jsApp);
m_jsEngine->globalObject().setProperty("application", scriptApp);

{
QJSValue scriptGeomContinuity = m_jsEngine->newObject();
scriptGeomContinuity.setProperty("C0", GeomAbs_C0);
scriptGeomContinuity.setProperty("G1", GeomAbs_G1);
scriptGeomContinuity.setProperty("C1", GeomAbs_C1);
scriptGeomContinuity.setProperty("G2", GeomAbs_G2);
scriptGeomContinuity.setProperty("C2", GeomAbs_C2);
scriptGeomContinuity.setProperty("C3", GeomAbs_C3);
scriptGeomContinuity.setProperty("CN", GeomAbs_CN);
m_jsEngine->globalObject().setProperty("GeomContinuity", scriptGeomContinuity);
}

{
QJSValue scriptGeomCurveType = m_jsEngine->newObject();
scriptGeomCurveType.setProperty("Line", GeomAbs_Line);
scriptGeomCurveType.setProperty("Circle", GeomAbs_Circle);
scriptGeomCurveType.setProperty("Ellipse", GeomAbs_Ellipse);
scriptGeomCurveType.setProperty("Hyperbola", GeomAbs_Hyperbola);
scriptGeomCurveType.setProperty("Parabola", GeomAbs_Parabola);
scriptGeomCurveType.setProperty("Bezier", GeomAbs_BezierCurve);
scriptGeomCurveType.setProperty("BSpline", GeomAbs_BSplineCurve);
scriptGeomCurveType.setProperty("Offset", GeomAbs_OffsetCurve);
scriptGeomCurveType.setProperty("Other", GeomAbs_OtherCurve);
m_jsEngine->globalObject().setProperty("GeomCurveType", scriptGeomCurveType);
}

{
QJSValue scriptGeomSurfaceType = m_jsEngine->newObject();
scriptGeomSurfaceType.setProperty("Plane", GeomAbs_Plane);
scriptGeomSurfaceType.setProperty("Cylinder", GeomAbs_Cylinder);
scriptGeomSurfaceType.setProperty("Cone", GeomAbs_Cone);
scriptGeomSurfaceType.setProperty("Sphere", GeomAbs_Sphere);
scriptGeomSurfaceType.setProperty("Torus", GeomAbs_Torus);
scriptGeomSurfaceType.setProperty("Bezier", GeomAbs_BezierSurface);
scriptGeomSurfaceType.setProperty("BSpline", GeomAbs_BSplineSurface);
scriptGeomSurfaceType.setProperty("SurfaceOfRevolution", GeomAbs_SurfaceOfRevolution);
scriptGeomSurfaceType.setProperty("SurfaceOfExtrusion", GeomAbs_SurfaceOfExtrusion);
scriptGeomSurfaceType.setProperty("Offset", GeomAbs_OffsetSurface);
scriptGeomSurfaceType.setProperty("Other", GeomAbs_OtherSurface);
m_jsEngine->globalObject().setProperty("GeomSurfaceType", scriptGeomSurfaceType);
}

{
QJSValue scriptShapeType = m_jsEngine->newObject();
scriptShapeType.setProperty("Compound", TopAbs_COMPOUND);
scriptShapeType.setProperty("CompoundSolid", TopAbs_COMPSOLID);
scriptShapeType.setProperty("Solid", TopAbs_SOLID);
scriptShapeType.setProperty("Shell", TopAbs_SHELL);
scriptShapeType.setProperty("Face", TopAbs_FACE);
scriptShapeType.setProperty("Wire", TopAbs_WIRE);
scriptShapeType.setProperty("Edge", TopAbs_EDGE);
scriptShapeType.setProperty("Vertex", TopAbs_VERTEX);
m_jsEngine->globalObject().setProperty("ShapeType", scriptShapeType);
}

{
QJSValue scriptShapeOrientation = m_jsEngine->newObject();
scriptShapeOrientation.setProperty("Forward", TopAbs_FORWARD);
scriptShapeOrientation.setProperty("Reversed", TopAbs_REVERSED);
scriptShapeOrientation.setProperty("Internal", TopAbs_INTERNAL);
scriptShapeOrientation.setProperty("External", TopAbs_EXTERNAL);
m_jsEngine->globalObject().setProperty("ShapeOrientation", scriptShapeOrientation);
}
const ApplicationPtr& app = this->context()->guiApp()->application();
m_jsEngine = createScriptEngine(app, this);
}

QFile jsFile(strFilePath);
Expand Down
4 changes: 2 additions & 2 deletions src/qtscripting/script_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ QVariant ScriptDocument::treeNode(unsigned int treeNodeId) const
return QVariant::fromValue(ScriptTreeNode(m_doc, treeNodeId));
}

void ScriptDocument::traverseShape(QJSValue shape, unsigned shapeTypeFiler, QJSValue fn)
void ScriptDocument::traverseShape(QJSValue shape, unsigned shapeTypeFilter, QJSValue fn)
{
if (!fn.isCallable())
return;

const auto shapeTypeEnum = static_cast<TopAbs_ShapeEnum>(shapeTypeFiler);
const auto shapeTypeEnum = static_cast<TopAbs_ShapeEnum>(shapeTypeFilter);
const auto scriptShape = m_jsApp->jsEngine()->fromScriptValue<ScriptShape>(shape);
BRepUtils::forEachSubShape(scriptShape.shape(), shapeTypeEnum, [&](const TopoDS_Shape& subShape) {
auto jsSubShape = m_jsApp->jsEngine()->toScriptValue(ScriptShape(subShape));
Expand Down
4 changes: 2 additions & 2 deletions src/qtscripting/script_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class ScriptDocument : public QObject {
unsigned entityTreeNodeId(int index) const;

Q_INVOKABLE void traverseModelTree(QJSValue fn);
Q_INVOKABLE QVariant treeNode(unsigned treeNodeId) const;
Q_INVOKABLE QVariant treeNode(unsigned treeNodeId) const; // ->ScriptTreeNode
// Q_INVOKABLE bool tagHasShapeColor(const QString& tag) const;
Q_INVOKABLE QColor tagShapeColor(const QString& tag) const;

Q_INVOKABLE void traverseShape(QJSValue shape, unsigned shapeTypeFiler, QJSValue fn);
Q_INVOKABLE void traverseShape(QJSValue shape, unsigned shapeTypeFilter, QJSValue fn);

signals:
void nameChanged();
Expand Down
72 changes: 72 additions & 0 deletions src/qtscripting/script_geom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/****************************************************************************
** Copyright (c) 2024, Fougue Ltd. <https://www.fougue.pro>
** All rights reserved.
** See license at https://github.com/fougue/mayo/blob/master/LICENSE.txt
****************************************************************************/

#include "script_geom.h"

#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>

namespace Mayo {

ScriptGeomAx3::ScriptGeomAx3(const gp_Ax3& ax3)
: m_ax3(ax3)
{
}

QVariant ScriptGeomAx3::location() const
{
return ScriptGeom::toScriptValue(m_ax3.Location());
}

QVariant ScriptGeomAx3::mainDirection() const
{
return ScriptGeom::toScriptValue(m_ax3.Direction());
}

QVariant ScriptGeomAx3::xDirection() const
{
return ScriptGeom::toScriptValue(m_ax3.XDirection());
}

QVariant ScriptGeomAx3::yDirection() const
{
return ScriptGeom::toScriptValue(m_ax3.YDirection());
}

namespace ScriptGeom {

static QVariant xyz_toScriptValue(const gp_XYZ& coords)
{
QVariantMap value;
value.insert("x", coords.X());
value.insert("y", coords.Y());
value.insert("z", coords.Z());
return value;
}

QVariant toScriptValue(const gp_Pnt& pnt)
{
return xyz_toScriptValue(pnt.XYZ());
}

QVariant toScriptValue(const gp_Vec& vec)
{
return xyz_toScriptValue(vec.XYZ());
}

QVariant toScriptValue(const gp_Dir& dir)
{
return xyz_toScriptValue(dir.XYZ());
}

QVariant toScriptValue(const gp_Ax3& ax3)
{
return QVariant::fromValue(ScriptGeomAx3(ax3));
}

} // namespace ScriptGeom

} // namespace Mayo
42 changes: 42 additions & 0 deletions src/qtscripting/script_geom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/****************************************************************************
** Copyright (c) 2024, Fougue Ltd. <https://www.fougue.pro>
** All rights reserved.
** See license at https://github.com/fougue/mayo/blob/master/LICENSE.txt
****************************************************************************/

#include <QtCore/QObject>
#include <QtCore/QVariant>
#include <gp_Ax3.hxx>

namespace Mayo {

class ScriptGeomAx3 {
Q_GADGET
Q_PROPERTY(QVariant location READ location)
Q_PROPERTY(QVariant mainDirection READ mainDirection)
Q_PROPERTY(QVariant xDirection READ xDirection)
Q_PROPERTY(QVariant yDirection READ yDirection)
public:
ScriptGeomAx3() = default;
ScriptGeomAx3(const gp_Ax3& ax3);

QVariant location() const;
QVariant mainDirection() const;
QVariant xDirection() const;
QVariant yDirection() const;

private:
gp_Ax3 m_ax3;
};

namespace ScriptGeom {

QVariant toScriptValue(const gp_Pnt& pnt);
QVariant toScriptValue(const gp_Vec& vec);
QVariant toScriptValue(const gp_Dir& dir);
QVariant toScriptValue(const gp_Ax3& ax3);

} // namespace ScriptGeom
} // namespace Mayo

Q_DECLARE_METATYPE(Mayo::ScriptGeomAx3)
19 changes: 5 additions & 14 deletions src/qtscripting/script_geom_curve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
****************************************************************************/

#include "script_geom_curve.h"
#include "script_geom.h"

namespace Mayo {

Expand All @@ -18,24 +19,14 @@ int ScriptGeomCurve::intervalCount(unsigned continuity) const
return m_curve.NbIntervals(static_cast<GeomAbs_Shape>(continuity));
}

QVariantMap ScriptGeomCurve::point(double u) const
QVariant ScriptGeomCurve::point(double u) const
{
const gp_Pnt pnt = m_curve.Value(u);
QVariantMap map;
map.insert("x", pnt.X());
map.insert("y", pnt.Y());
map.insert("z", pnt.Z());
return map;
return ScriptGeom::toScriptValue(m_curve.Value(u));
}

QVariantMap ScriptGeomCurve::dN(double u, int n) const
QVariant ScriptGeomCurve::dN(double u, int n) const
{
const gp_Vec vec = m_curve.DN(u, n);
QVariantMap map;
map.insert("x", vec.X());
map.insert("y", vec.Y());
map.insert("z", vec.Z());
return map;
return ScriptGeom::toScriptValue(m_curve.DN(u, n));
}

} // namespace Mayo
4 changes: 2 additions & 2 deletions src/qtscripting/script_geom_curve.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class ScriptGeomCurve {

double period() const { return m_curve.Period(); }

Q_INVOKABLE QVariantMap point(double u) const;
Q_INVOKABLE QVariantMap dN(double u, int n) const;
Q_INVOKABLE QVariant point(double u) const;
Q_INVOKABLE QVariant dN(double u, int n) const;

private:
BRepAdaptor_Curve m_curve;
Expand Down
Loading

0 comments on commit 6c07101

Please sign in to comment.