Assembly: Replace ObjRefPairs std::pair by structure.

This commit is contained in:
PaddleStroke
2024-07-24 10:17:00 +02:00
committed by Yorik van Havre
parent 4f79c675b7
commit a5505a1b1c
3 changed files with 42 additions and 40 deletions

View File

@@ -725,11 +725,10 @@ bool AssemblyObject::isJointTypeConnecting(App::DocumentObject* joint)
}
bool AssemblyObject::isObjInSetOfObjRefPairs(App::DocumentObject* obj,
const std::set<ObjRefPair>& set)
bool AssemblyObject::isObjInSetOfObjRefs(App::DocumentObject* obj, const std::vector<ObjRef>& set)
{
for (const auto& pair : set) {
if (pair.first == obj) {
if (pair.obj == obj) {
return true;
}
}
@@ -739,11 +738,11 @@ bool AssemblyObject::isObjInSetOfObjRefPairs(App::DocumentObject* obj,
void AssemblyObject::removeUnconnectedJoints(std::vector<App::DocumentObject*>& joints,
std::vector<App::DocumentObject*> groundedObjs)
{
std::set<ObjRefPair> connectedParts;
std::vector<ObjRef> connectedParts;
// Initialize connectedParts with groundedObjs
for (auto* groundedObj : groundedObjs) {
connectedParts.insert({groundedObj, nullptr});
connectedParts.push_back({groundedObj, nullptr});
}
// Perform a traversal from each grounded object
@@ -759,8 +758,8 @@ void AssemblyObject::removeUnconnectedJoints(std::vector<App::DocumentObject*>&
[&](App::DocumentObject* joint) {
App::DocumentObject* obj1 = getMovingPartFromRef(joint, "Reference1");
App::DocumentObject* obj2 = getMovingPartFromRef(joint, "Reference2");
if (!isObjInSetOfObjRefPairs(obj1, connectedParts)
|| !isObjInSetOfObjRefPairs(obj2, connectedParts)) {
if (!isObjInSetOfObjRefs(obj1, connectedParts)
|| !isObjInSetOfObjRefs(obj2, connectedParts)) {
Base::Console().Warning(
"%s is unconnected to a grounded part so it is ignored.\n",
joint->getFullName());
@@ -772,25 +771,25 @@ void AssemblyObject::removeUnconnectedJoints(std::vector<App::DocumentObject*>&
}
void AssemblyObject::traverseAndMarkConnectedParts(App::DocumentObject* currentObj,
std::set<ObjRefPair>& connectedParts,
std::vector<ObjRef>& connectedParts,
const std::vector<App::DocumentObject*>& joints)
{
// getConnectedParts returns the objs connected to the currentObj by any joint
auto connectedObjs = getConnectedParts(currentObj, joints);
for (auto nextObjRef : connectedObjs) {
if (!isObjInSetOfObjRefPairs(nextObjRef.first, connectedParts)) {
// Create a new ObjRefPair with the nextObj and a nullptr for PropertyXLinkSub*
connectedParts.insert(nextObjRef);
traverseAndMarkConnectedParts(nextObjRef.first, connectedParts, joints);
for (auto& nextObjRef : connectedObjs) {
if (!isObjInSetOfObjRefs(nextObjRef.obj, connectedParts)) {
// Create a new ObjRef with the nextObj and a nullptr for PropertyXLinkSub*
connectedParts.push_back(nextObjRef);
traverseAndMarkConnectedParts(nextObjRef.obj, connectedParts, joints);
}
}
}
std::vector<ObjRefPair>
std::vector<ObjRef>
AssemblyObject::getConnectedParts(App::DocumentObject* part,
const std::vector<App::DocumentObject*>& joints)
{
std::vector<ObjRefPair> connectedParts;
std::vector<ObjRef> connectedParts;
for (auto joint : joints) {
if (!isJointTypeConnecting(joint)) {
continue;
@@ -836,11 +835,11 @@ bool AssemblyObject::isPartConnected(App::DocumentObject* obj)
std::vector<App::DocumentObject*> groundedObjs = getGroundedParts();
std::vector<App::DocumentObject*> joints = getJoints(false);
std::set<ObjRefPair> connectedParts;
std::vector<ObjRef> connectedParts;
// Initialize connectedParts with groundedObjs
for (auto* groundedObj : groundedObjs) {
connectedParts.insert({groundedObj, nullptr});
connectedParts.push_back({groundedObj, nullptr});
}
// Perform a traversal from each grounded object
@@ -848,8 +847,8 @@ bool AssemblyObject::isPartConnected(App::DocumentObject* obj)
traverseAndMarkConnectedParts(groundedObj, connectedParts, joints);
}
for (auto objRef : connectedParts) {
if (obj == objRef.first) {
for (auto& objRef : connectedParts) {
if (obj == objRef.obj) {
return true;
}
}
@@ -1515,8 +1514,8 @@ std::shared_ptr<ASMTMarker> AssemblyObject::makeMbdMarker(std::string& name, Bas
return mbdMarker;
}
std::vector<std::pair<App::DocumentObject*, App::PropertyXLinkSub*>>
AssemblyObject::getDownstreamParts(App::DocumentObject* part, App::DocumentObject* joint)
std::vector<ObjRef> AssemblyObject::getDownstreamParts(App::DocumentObject* part,
App::DocumentObject* joint)
{
// First we deactivate the joint
bool state = getJointActivated(joint);
@@ -1524,13 +1523,12 @@ AssemblyObject::getDownstreamParts(App::DocumentObject* part, App::DocumentObjec
std::vector<App::DocumentObject*> joints = getJoints(false);
std::set<std::pair<App::DocumentObject*, App::PropertyXLinkSub*>> connectedParts = {
{part, nullptr}};
std::vector<ObjRef> connectedParts = {{part, nullptr}};
traverseAndMarkConnectedParts(part, connectedParts, joints);
std::vector<std::pair<App::DocumentObject*, App::PropertyXLinkSub*>> downstreamParts;
for (auto parti : connectedParts) {
if (!isPartConnected(parti.first) && (parti.first != part)) {
std::vector<ObjRef> downstreamParts;
for (auto& parti : connectedParts) {
if (!isPartConnected(parti.obj) && (parti.obj != part)) {
downstreamParts.push_back(parti);
}
}

View File

@@ -62,7 +62,11 @@ namespace Assembly
class JointGroup;
class ViewGroup;
using ObjRefPair = std::pair<App::DocumentObject*, App::PropertyXLinkSub*>;
struct ObjRef
{
App::DocumentObject* obj;
App::PropertyXLinkSub* ref;
};
// This enum has to be the same as the one in JointObject.py
enum class JointType
@@ -202,19 +206,18 @@ public:
bool isJointConnectingPartToGround(App::DocumentObject* joint, const char* partPropName);
bool isJointTypeConnecting(App::DocumentObject* joint);
bool isObjInSetOfObjRefPairs(App::DocumentObject* obj, const std::set<ObjRefPair>& pairs);
bool isObjInSetOfObjRefs(App::DocumentObject* obj, const std::vector<ObjRef>& pairs);
void removeUnconnectedJoints(std::vector<App::DocumentObject*>& joints,
std::vector<App::DocumentObject*> groundedObjs);
void traverseAndMarkConnectedParts(App::DocumentObject* currentPart,
std::set<ObjRefPair>& connectedParts,
std::vector<ObjRef>& connectedParts,
const std::vector<App::DocumentObject*>& joints);
std::vector<ObjRefPair> getConnectedParts(App::DocumentObject* part,
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<ObjRefPair> getDownstreamParts(App::DocumentObject* part,
App::DocumentObject* joint);
std::vector<ObjRef> getDownstreamParts(App::DocumentObject* part, App::DocumentObject* joint);
std::vector<App::DocumentObject*> getUpstreamParts(App::DocumentObject* part, int limit = 0);
App::DocumentObject* getUpstreamMovingPart(App::DocumentObject* part,
App::DocumentObject*& joint,

View File

@@ -712,22 +712,23 @@ ViewProviderAssembly::DragMode ViewProviderAssembly::findDragMode()
jcsGlobalPlc = global_plc * jcsPlc;
// Add downstream parts so that they move together
auto downstreamParts = assemblyPart->getDownstreamParts(docsToMove[0].obj, movingJoint);
for (auto partRef : downstreamParts) {
auto* pPlc = dynamic_cast<App::PropertyPlacement*>(
partRef.first->getPropertyByName("Placement"));
std::vector<Assembly::ObjRef> downstreamParts =
assemblyPart->getDownstreamParts(docsToMove[0].obj, movingJoint);
for (auto& partRef : downstreamParts) {
auto* pPlc =
dynamic_cast<App::PropertyPlacement*>(partRef.obj->getPropertyByName("Placement"));
if (pPlc) {
App::DocumentObject* selRoot = partRef.second->getValue();
App::DocumentObject* selRoot = partRef.ref->getValue();
if (!selRoot) {
return DragMode::None;
}
std::vector<std::string> subs = partRef.second->getSubValues();
std::vector<std::string> subs = partRef.ref->getSubValues();
if (subs.empty()) {
return DragMode::None;
}
docsToMove.emplace_back(partRef.first, pPlc->getValue(), selRoot, subs[0]);
docsToMove.emplace_back(partRef.obj, pPlc->getValue(), selRoot, subs[0]);
}
}