Files
create/src/Mod/Assembly/App/AssemblyObject.h
forbes acc255972d
Some checks failed
Build and Test / build (pull_request) Failing after 7m52s
feat(assembly): fixed reference planes + solver docs
Assembly Origin Planes:
- AssemblyObject::setupObject() relabels origin planes to
  Top (XY), Front (XZ), Right (YZ) on assembly creation
- CommandCreateAssembly.py makes origin planes visible by default
- AssemblyUtils.cpp getObjFromRef() resolves LocalCoordinateSystem
  to child datum elements for joint references to origin planes
- TestAssemblyOriginPlanes.py: 9 integration tests covering
  structure, labels, grounding, reference resolution, solver,
  and save/load round-trip

Solver Documentation:
- docs/src/solver/: 7 new pages covering architecture overview,
  expression DAG, constraints, solving algorithms, diagnostics,
  assembly integration, and writing custom solvers
- docs/src/SUMMARY.md: added Kindred Solver section
2026-02-21 09:09:16 -06:00

300 lines
10 KiB
C++

// SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
* *
* Copyright (c) 2023 Ondsel <development@ondsel.com> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD 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. *
* *
* FreeCAD 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 FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
***************************************************************************/
#ifndef ASSEMBLY_AssemblyObject_H
#define ASSEMBLY_AssemblyObject_H
#include <memory>
#include <boost/signals2.hpp>
#include <Mod/Assembly/AssemblyGlobal.h>
#include <Mod/Assembly/Solver/Types.h>
#include <App/FeaturePython.h>
#include <App/Part.h>
#include <App/PropertyLinks.h>
namespace KCSolve
{
class IKCSolver;
} // namespace KCSolve
namespace App
{
class PropertyXLinkSub;
} // namespace App
namespace Base
{
class Placement;
class Rotation;
} // namespace Base
namespace Assembly
{
class AssemblyLink;
class JointGroup;
class ViewGroup;
enum class JointType;
struct ObjRef
{
App::DocumentObject* obj;
App::PropertyXLinkSub* ref;
};
class AssemblyExport AssemblyObject: public App::Part
{
PROPERTY_HEADER_WITH_OVERRIDE(Assembly::AssemblyObject);
public:
AssemblyObject();
~AssemblyObject() override;
PyObject* getPyObject() override;
/// returns the type name of the ViewProvider
const char* getViewProviderName() const override
{
return "AssemblyGui::ViewProviderAssembly";
}
void setupObject() override;
App::DocumentObjectExecReturn* execute() override;
void onChanged(const App::Property* prop) override;
/* Solve the assembly. It will update first the joints, solve, update placements of the parts
and redraw the joints Args : enableRedo : This store initial positions to enable undo while
being in an active transaction (joint creation).*/
int solve(bool enableRedo = false, bool updateJCS = true);
int generateSimulation(App::DocumentObject* sim);
int updateForFrame(size_t index, bool updateJCS = true);
size_t numberOfFrames();
void preDrag(std::vector<App::DocumentObject*> dragParts);
void doDragStep();
void postDrag();
void savePlacementsForUndo();
void undoSolve();
void resetSolver();
void clearUndo();
void exportAsASMT(std::string fileName);
/// Build the assembly constraint graph without solving.
/// Returns an empty SolveContext if no parts are grounded.
KCSolve::SolveContext getSolveContext();
bool validateNewPlacements();
void setNewPlacements();
static void redrawJointPlacements(std::vector<App::DocumentObject*> joints);
static void redrawJointPlacement(App::DocumentObject* joint);
// This makes sure that LinkGroups or sub-assemblies have identity placements.
void ensureIdentityPlacements();
int slidingPartIndex(App::DocumentObject* joint);
JointGroup* getJointGroup() const;
ViewGroup* getExplodedViewGroup() const;
template<typename T>
T* getGroup();
std::vector<App::DocumentObject*> getJoints(
bool updateJCS = true,
bool delBadJoints = false,
bool subJoints = true
);
std::vector<App::DocumentObject*> getGroundedJoints();
std::vector<App::DocumentObject*> getJointsOfObj(App::DocumentObject* obj);
std::vector<App::DocumentObject*> getJointsOfPart(App::DocumentObject* part);
App::DocumentObject* getJointOfPartConnectingToGround(
App::DocumentObject* part,
std::string& name,
const std::vector<App::DocumentObject*>& excludeJoints = {}
);
std::unordered_set<App::DocumentObject*> getGroundedParts();
bool isJointConnectingPartToGround(App::DocumentObject* joint, const char* partPropName);
bool isJointTypeConnecting(App::DocumentObject* joint);
bool isObjInSetOfObjRefs(App::DocumentObject* obj, const std::vector<ObjRef>& pairs);
void removeUnconnectedJoints(
std::vector<App::DocumentObject*>& joints,
std::unordered_set<App::DocumentObject*> groundedObjs
);
void traverseAndMarkConnectedParts(
App::DocumentObject* currentPart,
std::vector<ObjRef>& connectedParts,
const std::vector<App::DocumentObject*>& joints
);
std::vector<ObjRef> getConnectedParts(
App::DocumentObject* part,
const std::vector<App::DocumentObject*>& joints
);
bool isPartGrounded(App::DocumentObject* part);
bool isPartConnected(App::DocumentObject* part);
std::vector<ObjRef> getDownstreamParts(
App::DocumentObject* part,
App::DocumentObject* joint = nullptr
);
App::DocumentObject* getUpstreamMovingPart(
App::DocumentObject* part,
App::DocumentObject*& joint,
std::string& name,
std::vector<App::DocumentObject*> excludeJoints = {}
);
double getObjMass(App::DocumentObject* obj);
void setObjMasses(std::vector<std::pair<App::DocumentObject*, double>> objectMasses);
std::vector<AssemblyLink*> getSubAssemblies();
std::vector<App::DocumentObject*> getMotionsFromSimulation(App::DocumentObject* sim);
bool isJointValid(App::DocumentObject* joint);
bool isEmpty() const;
int numberOfComponents() const;
void updateSolveStatus();
inline int getLastDoF() const
{
return lastDoF;
}
inline bool getLastHasConflicts() const
{
return lastHasConflict;
}
inline bool getLastHasRedundancies() const
{
return lastHasRedundancies;
}
inline bool getLastHasPartialRedundancies() const
{
return lastHasPartialRedundancies;
}
inline bool getLastHasMalformedConstraints() const
{
return lastHasMalformedConstraints;
}
inline int getLastSolverStatus() const
{
return lastSolverStatus;
}
inline const std::vector<std::string>& getLastConflicting() const
{
return lastConflictingJoints;
}
inline const std::vector<std::string>& getLastRedundant() const
{
return lastRedundantJoints;
}
inline const std::vector<std::string>& getLastPartiallyRedundant() const
{
return lastPartialRedundantJoints;
}
inline const std::vector<std::string>& getLastMalformed() const
{
return lastMalformedJoints;
}
fastsignals::signal<void()> signalSolverUpdate;
private:
// ── Solver integration ─────────────────────────────────────────
KCSolve::IKCSolver* getOrCreateSolver();
KCSolve::SolveContext buildSolveContext(
const std::vector<App::DocumentObject*>& joints,
bool forSimulation = false,
App::DocumentObject* sim = nullptr
);
KCSolve::Transform computeMarkerTransform(
App::DocumentObject* joint,
const char* propRefName,
const char* propPlcName
);
struct RackPinionResult
{
std::string partIdI;
KCSolve::Transform markerI;
std::string partIdJ;
KCSolve::Transform markerJ;
};
RackPinionResult computeRackPinionMarkers(App::DocumentObject* joint);
// ── Part ↔ solver ID mapping ───────────────────────────────────
// Maps a solver part ID to the FreeCAD objects it represents.
// Multiple objects map to one ID when parts are bundled by Fixed joints.
struct PartMapping
{
App::DocumentObject* obj;
Base::Placement offset; // identity for primary, non-identity for bundled
};
std::unordered_map<std::string, std::vector<PartMapping>> partIdToObjs_;
std::unordered_map<App::DocumentObject*, std::string> objToPartId_;
// Register a part (and recursively its fixed-joint bundle when bundleFixed is set).
// Returns the solver part ID.
std::string registerPart(App::DocumentObject* obj);
// ── Solver state ───────────────────────────────────────────────
std::unique_ptr<KCSolve::IKCSolver> solver_;
KCSolve::SolveResult lastResult_;
// ── Existing state (unchanged) ─────────────────────────────────
std::vector<std::pair<App::DocumentObject*, double>> objMasses;
std::vector<App::DocumentObject*> draggedParts;
std::vector<std::pair<App::DocumentObject*, Base::Placement>> previousPositions;
bool bundleFixed;
int lastDoF;
bool lastHasConflict;
bool lastHasRedundancies;
bool lastHasPartialRedundancies;
bool lastHasMalformedConstraints;
int lastSolverStatus;
std::vector<std::string> lastRedundantJoints;
std::vector<std::string> lastConflictingJoints;
std::vector<std::string> lastPartialRedundantJoints;
std::vector<std::string> lastMalformedJoints;
};
} // namespace Assembly
#endif // ASSEMBLY_AssemblyObject_H