Assembly: Enable ungrounded connected parts to move as one item.

This commit is contained in:
PaddleStroke
2024-08-05 08:21:08 +02:00
committed by Yorik van Havre
parent 8d622e02ae
commit 62e5e0e41d
5 changed files with 131 additions and 64 deletions

View File

@@ -454,9 +454,9 @@ App::DocumentObject* AssemblyObject::getJointOfPartConnectingToGround(App::Docum
return nullptr;
}
JointGroup* AssemblyObject::getJointGroup()
JointGroup* AssemblyObject::getJointGroup(App::Part* part)
{
App::Document* doc = getDocument();
App::Document* doc = part->getDocument();
std::vector<DocumentObject*> jointGroups =
doc->getObjectsOfType(Assembly::JointGroup::getClassTypeId());
@@ -464,13 +464,18 @@ JointGroup* AssemblyObject::getJointGroup()
return nullptr;
}
for (auto jointGroup : jointGroups) {
if (hasObject(jointGroup)) {
if (part->hasObject(jointGroup)) {
return dynamic_cast<JointGroup*>(jointGroup);
}
}
return nullptr;
}
JointGroup* AssemblyObject::getJointGroup()
{
return getJointGroup(this);
}
ViewGroup* AssemblyObject::getExplodedViewGroup()
{
App::Document* doc = getDocument();
@@ -487,7 +492,8 @@ ViewGroup* AssemblyObject::getExplodedViewGroup()
return nullptr;
}
std::vector<App::DocumentObject*> AssemblyObject::getJoints(bool updateJCS, bool delBadJoints)
std::vector<App::DocumentObject*>
AssemblyObject::getJoints(bool updateJCS, bool delBadJoints, bool subJoints)
{
std::vector<App::DocumentObject*> joints = {};
@@ -528,9 +534,11 @@ std::vector<App::DocumentObject*> AssemblyObject::getJoints(bool updateJCS, bool
}
// add sub assemblies joints.
for (auto& assembly : getSubAssemblies()) {
auto subJoints = assembly->getJoints(updateJCS, delBadJoints);
joints.insert(joints.end(), subJoints.begin(), subJoints.end());
if (subJoints) {
for (auto& assembly : getSubAssemblies()) {
auto subJoints = assembly->getJoints();
joints.insert(joints.end(), subJoints.begin(), subJoints.end());
}
}
// Make sure the joints are up to date.
@@ -1518,8 +1526,11 @@ std::vector<ObjRef> AssemblyObject::getDownstreamParts(App::DocumentObject* part
App::DocumentObject* joint)
{
// First we deactivate the joint
bool state = getJointActivated(joint);
setJointActivated(joint, false);
bool state = false;
if (joint) {
state = getJointActivated(joint);
setJointActivated(joint, false);
}
std::vector<App::DocumentObject*> joints = getJoints(false);
@@ -1533,7 +1544,9 @@ std::vector<ObjRef> AssemblyObject::getDownstreamParts(App::DocumentObject* part
}
}
AssemblyObject::setJointActivated(joint, state);
if (joint) {
AssemblyObject::setJointActivated(joint, state);
}
/*if (limit > 1000) { // Infinite loop protection
return {};
}
@@ -2177,14 +2190,15 @@ App::DocumentObject* AssemblyObject::getObjFromProp(App::DocumentObject* joint,
return propObj->getValue();
}
App::DocumentObject* AssemblyObject::getObjFromRef(App::PropertyXLinkSub* prop)
App::DocumentObject* AssemblyObject::getObjFromRef(App::DocumentObject* obj, std::string& sub)
{
if (!prop) {
if (!obj) {
return nullptr;
}
App::Document* doc = prop->getValue()->getDocument();
std::vector<std::string> names = getSubAsList(prop);
App::Document* doc = obj->getDocument();
std::vector<std::string> names = splitSubName(sub);
// Lambda function to check if the typeId is a BodySubObject
auto isBodySubObject = [](App::DocumentObject* obj) -> bool {
@@ -2217,6 +2231,10 @@ App::DocumentObject* AssemblyObject::getObjFromRef(App::PropertyXLinkSub* prop)
return nullptr;
}
if (obj->isDerivedFrom<App::DocumentObjectGroup>()) {
continue;
}
// The last but one name should be the selected
if (std::next(it) == std::prev(names.end())) {
return obj;
@@ -2251,6 +2269,25 @@ App::DocumentObject* AssemblyObject::getObjFromRef(App::PropertyXLinkSub* prop)
return nullptr;
}
App::DocumentObject* AssemblyObject::getObjFromRef(App::PropertyXLinkSub* prop)
{
if (!prop) {
return nullptr;
}
App::DocumentObject* obj = prop->getValue();
if (!obj) {
return nullptr;
}
std::vector<std::string> subs = prop->getSubValues();
if (subs.empty()) {
return nullptr;
}
return getObjFromRef(obj, subs[0]);
}
App::DocumentObject* AssemblyObject::getObjFromRef(App::DocumentObject* joint, const char* pName)
{
auto* prop = dynamic_cast<App::PropertyXLinkSub*>(joint->getPropertyByName(pName));