Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull and push Rigid constraints #297

Merged
merged 7 commits into from
Oct 3, 2023
45 changes: 31 additions & 14 deletions GSA_Adapter/CRUD/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using BH.oM.Adapters.GSA.SurfaceProperties;
using BH.oM.Structure.MaterialFragments;
using BH.oM.Structure.Fragments;
using BH.oM.Adapters.GSA.Elements;

namespace BH.Adapter.GSA
{
Expand Down Expand Up @@ -159,32 +160,47 @@ private bool CreateObject(ISectionProperty prop)
//
private bool CreateLinks(IEnumerable<RigidLink> links)
{

bool success = true;
foreach (RigidLink link in links)
{
success &= ComCall(Convert.ToGsaString(link, GetAdapterId<int>(link).ToString(), 0));
}

foreach (RigidLink link in links)
{
List<int> allIds = new List<int>();
for (int i = 1; i < link.SecondaryNodes.Count; i++)
if (Convert.CheckRigCon(link) == false)
{
int id = (int)NextFreeId(link.GetType(), i == 1);
success &= ComCall(Convert.ToGsaString(link, id.ToString(), i));
allIds.Add(id);
if (link.SecondaryNodes.Count > 1)
{
List<int> secondaryIds = new List<int>();
foreach (Node secondaryNode in link.SecondaryNodes)
{
int id = (int)NextFreeId(link.GetType(), false);
int secondaryIndex = link.SecondaryNodes.IndexOf(secondaryNode);
success &= ComCall(Convert.ToGsaString(link, id.ToString(), secondaryIndex));
secondaryIds.Add(id);
}

var allIds = new List<int> { GetAdapterId<int>(link) };
allIds.AddRange(secondaryIds);
link.Fragments.Remove(typeof(GSAId));
link.SetAdapterId(typeof(GSAId), allIds);
}
else
{
int id = (int)NextFreeId(link.GetType(), false);
success &= ComCall(Convert.ToGsaString(link, id.ToString()));
link.Fragments.Remove(typeof(GSAId));
link.SetAdapterId(typeof(GSAId), id);
}
}
if (link.SecondaryNodes.Count > 1)
else
{
allIds.Insert(0, GetAdapterId<int>(link));
link.Fragments.Remove(typeof(GSAId)); // to remove the existing single id on the link
link.SetAdapterId(typeof(GSAId), allIds);
success &= ComCall(Convert.ToGsaString(link, GetAdapterId<int>(link).ToString()));
}
}

return success;
}



/***************************************************/

private bool CreateFEMesh(FEMesh mesh)
Expand Down Expand Up @@ -278,6 +294,7 @@ private bool CreateObject(FabricPanelProperty fabricProperty)


/***************************************************/

}
}

Expand Down
5 changes: 4 additions & 1 deletion GSA_Adapter/CRUD/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,14 @@ public List<RigidLink> ReadRigidLink(List<string> ids = null)
Dictionary<int, LinkConstraint> constraints = GetCachedOrReadAsDictionary<int, LinkConstraint>();
Dictionary<int, Node> nodes = GetCachedOrReadAsDictionary<int, Node>();

string allConstr = m_gsaCom.GwaCommand("GET_ALL, RIGID").ToString();
string[] constrArr = string.IsNullOrWhiteSpace(allConstr) ? new string[0] : allConstr.Split('\n');

int[] potentialBeamRefs = GenerateIndices(ids, typeof(RigidLink));
GsaElement[] gsaElements = new GsaElement[potentialBeamRefs.Length];
m_gsaCom.Elements(potentialBeamRefs, out gsaElements);

return Convert.FromGsaRigidLinks(gsaElements, constraints, nodes);
return Convert.FromGsaRigidLinks(gsaElements, constrArr, constraints, nodes);

//if (ids == null)
// return proArr.Select(x => Convert.FromGsaSectionProperty(x, materials)).ToList();
Expand Down
101 changes: 101 additions & 0 deletions GSA_Adapter/Convert/FromGsa/Elements/RigidConstraint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM 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 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM 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 this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

#if GSA_10_1
using Interop.Gsa_10_1;
#else
using Interop.gsa_8_7;
#endif
using BH.Engine.Serialiser;
using BH.Engine.Adapter;
using BH.oM.Adapters.GSA;
using BH.oM.Structure.Elements;
using BH.oM.Structure.Constraints;
using System.Collections.Generic;
using BH.oM.Adapters.GSA.Elements;
using System;
using System.Linq;

namespace BH.Adapter.GSA
{
public static partial class Convert
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

public static List<RigidConstraint> FromGsaRigidConstraint(IEnumerable<string> gsaStrings, Dictionary<int, Node> nodes)
{
List<RigidConstraint> constraintList = new List<RigidConstraint>();

foreach (string gsaString in gsaStrings)
{
string[] tokens = gsaString.Split(',');

int primaryNodeName = int.Parse(tokens[2]);
string linkTypeString = tokens[3];
string[] constrainedNodeNames = tokens[4].Split(',');

Node primaryNode;
nodes.TryGetValue(primaryNodeName, out primaryNode);

List<Node> constrainedNodes = new List<Node>();

foreach (string constrainedNodeName in constrainedNodeNames)
{
if (int.TryParse(constrainedNodeName, out int constrainedNodeId))
{
if (nodes.TryGetValue(constrainedNodeId, out Node constrainedNode))
constrainedNodes.Add(constrainedNode);
}
}

RigidConstraintLinkType linkType = RigidConstraintLinkType.ALL;

if (Enum.TryParse(linkTypeString, out RigidConstraintLinkType parsedLinkType))
linkType = parsedLinkType;

RigidConstraint constraint = new RigidConstraint()
{
PrimaryNode = primaryNode,
ConstrainedNodes = constrainedNodes,
Type = linkType
};

constraintList.Add(constraint);
}

return constraintList;
}





/***************************************************/

}
}




148 changes: 146 additions & 2 deletions GSA_Adapter/Convert/FromGsa/Elements/RigidLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
using BH.oM.Structure.Elements;
using BH.oM.Structure.Constraints;
using System.Collections.Generic;

using System.Linq;

namespace BH.Adapter.GSA
{
Expand All @@ -41,10 +41,12 @@ public static partial class Convert
/**** Public Methods ****/
/***************************************************/

public static List<RigidLink> FromGsaRigidLinks(IEnumerable<GsaElement> gsaElements, Dictionary<int, LinkConstraint> constraints, Dictionary<int, Node> nodes)
public static List<RigidLink> FromGsaRigidLinks(IEnumerable<GsaElement> gsaElements, IEnumerable<string> gsaStrings, Dictionary<int, LinkConstraint> constraints, Dictionary<int, Node> nodes)
{

List<RigidLink> linkList = new List<RigidLink>();

//Rigid Link
foreach (GsaElement gsaLink in gsaElements)
{
if (gsaLink.eType != 9)
Expand All @@ -61,8 +63,150 @@ public static List<RigidLink> FromGsaRigidLinks(IEnumerable<GsaElement> gsaEleme
int id = gsaLink.Ref;
link.SetAdapterId(typeof(GSAId), id);
linkList.Add(link);
}

//Rigid Constraint
foreach (string gsaString in gsaStrings)
{
string[] tokens = gsaString.Split(',');

string linkName = tokens[1];
int primaryNodeName = int.Parse(tokens[2]);
string linkTypeString = tokens[3];
string[] constrainedNodeNames = tokens[4].Split(' ');

Node primaryNode;
nodes.TryGetValue(primaryNodeName, out primaryNode);

List<Node> constrainedNodes = new List<Node>();

foreach (string constrainedNodeName in constrainedNodeNames)
{
if (int.TryParse(constrainedNodeName, out int constrainedNodeId))
{
if (nodes.TryGetValue(constrainedNodeId, out Node constrainedNode))
constrainedNodes.Add(constrainedNode);
}
}

LinkConstraint linkType;

switch (linkTypeString)
{
case "ALL":
linkType = Engine.Structure.Create.LinkConstraintFixed();
break;
case "PIN":
linkType = Engine.Structure.Create.LinkConstraintPinned();
break;
case "XY_PLANE":
linkType = Engine.Structure.Create.LinkConstraintXYPlane();
break;
case "ZX_PLANE":
linkType = Engine.Structure.Create.LinkConstraintZXPlane();
break;
case "YZ_PLANE":
linkType = Engine.Structure.Create.LinkConstraintYZPlane();
break;
case "XY_PLANE_PIN":
linkType = Engine.Structure.Create.LinkConstraintXYPlanePin();
break;
case "ZX_PLANE_PIN":
linkType = Engine.Structure.Create.LinkConstraintZXPlanePin();
break;
case "YZ_PLANE_PIN":
linkType = Engine.Structure.Create.LinkConstraintYZPlanePin();
break;
case "XY_PLATE":
linkType = Engine.Structure.Create.LinkConstraintYPlateZPlate(); //Wrong name in engine. Should be just ZPlate.
break;
case "ZX_PLATE":
linkType = Engine.Structure.Create.LinkConstraintYPlate();
break;
case "YZ_PLATE":
linkType = Engine.Structure.Create.LinkConstraintXPlate();
break;
case "XY_PLATE_PIN":
linkType = Engine.Structure.Create.LinkConstraintZPlatePin();
break;
case "ZX_PLATE_PIN":
linkType = Engine.Structure.Create.LinkConstraintYPlatePin();
break;
case "YZ_PLATE_PIN":
linkType = Engine.Structure.Create.LinkConstraintXPlatePin();
break;
default:
//String in format example: X:XYY-Y:YZZXX-Z:YY-XX:XX-YY:YY-ZZ:ZZ
linkType = new LinkConstraint();
string[] constraintProps = linkTypeString.Split('-');

foreach (string c in constraintProps)
{
string[] fromTo = c.Split(':');
string from = fromTo[0];
string to = fromTo[1];
switch (from)
{
case "X":
if (to.Contains('X'))
linkType.XtoX = true;
if (to.Contains('Y'))
linkType.XtoYY = true;
if (to.Contains('Z'))
linkType.XtoZZ = true;
break;
case "Y":
if (to.Contains('X'))
linkType.YtoXX = true;
if (to.Contains('Y'))
linkType.YtoY = true;
if (to.Contains('Z'))
linkType.YtoZZ = true;
break;
case "Z":
if (to.Contains('X'))
linkType.ZtoXX = true;
if (to.Contains('Y'))
linkType.ZtoYY = true;
if (to.Contains('Z'))
linkType.ZtoZ = true;
break;
case "XX":
if (to.Contains("XX"))
linkType.XXtoXX = true;
break;
case "YY":
if (to.Contains("YY"))
linkType.YYtoYY = true;
break;
case "ZZ":
if (to.Contains("ZZ"))
linkType.ZZtoZZ = true;
break;
}
}
break;
}

RigidLink link = new RigidLink()
{
PrimaryNode = primaryNode,
SecondaryNodes = constrainedNodes,
Constraint = linkType
};

link.ApplyTaggedName(linkName);

IsRigidConstraint RCtag = new IsRigidConstraint
{
RigidConstraint = true
};

link.Fragments.Add(RCtag);

linkList.Add(link);
}

return linkList;
}

Expand Down
Loading