public DS.Mesh MeshToNative(Mesh mesh) { var points = ArrayToPointList(mesh.vertices, mesh.units); List <IndexGroup> faces = new List <IndexGroup>(); int i = 0; while (i < mesh.faces.Count) { if (mesh.faces[i] == 0) { // triangle var ig = IndexGroup.ByIndices((uint)mesh.faces[i + 1], (uint)mesh.faces[i + 2], (uint)mesh.faces[i + 3]); faces.Add(ig); i += 4; } else { // quad var ig = IndexGroup.ByIndices((uint)mesh.faces[i + 1], (uint)mesh.faces[i + 2], (uint)mesh.faces[i + 3], (uint)mesh.faces[i + 4]); faces.Add(ig); i += 5; } } var dsMesh = DS.Mesh.ByPointsFaceIndices(points, faces); dsMesh.SetDynamoProperties <DS.Mesh>(GetDynamicMembersFromBase(mesh)); return(dsMesh); }
public TexturedMeshBinder(Autodesk.Dynamo.MeshToolkit.Mesh mesh, Color color, string textureFileName, Vector2Collection textureCoordinates) { StartingPositions = mesh.Vertices().ToTriples().ToArray(); Color = color; try { diffuseMap = new BitmapImage(new Uri(textureFileName)); } catch (FileNotFoundException) { throw new Exception("Could not locate the texture file"); } this.textureCoordinates = textureCoordinates; faceIndices = mesh.VertexIndicesByTri(); int faceCount = faceIndices.Count / 3; faces = new IndexGroup[faceCount]; for (int i = 0; i < faceCount; i++) { faces[i] = IndexGroup.ByIndices( (uint)faceIndices[i * 3], (uint)faceIndices[i * 3 + 1], (uint)faceIndices[i * 3 + 2]); } meshFaceIndices = new IntCollection(); foreach (IndexGroup face in faces) { meshFaceIndices.Add((int)face.A); meshFaceIndices.Add((int)face.B); meshFaceIndices.Add((int)face.C); } }
public void ByMeshNameCategoryMaterial_ValidInput() { var p1 = Point.ByCoordinates(0.0, 0.0, 0.0); var p2 = Point.ByCoordinates(1.0, 1.0, 0); var p3 = Point.ByCoordinates(2.0, 0, 0); var index1 = IndexGroup.ByIndices(0, 1, 2); var mesh = Mesh.ByPointsFaceIndices(new List <Point>() { p1, p2, p3 }, new List <IndexGroup>() { index1 }); var mat = DocumentManager.Instance.ElementsOfType <Autodesk.Revit.DB.Material>().First(); var ds = DirectShape.ByMesh(mesh, Category.ByName("OST_GenericModel"), Material.ByName(mat.Name), "a mesh"); Assert.NotNull(ds); Assert.AreEqual("a mesh", ds.Name); Assert.AreEqual((mesh.Tags.LookupTag(ds.InternalElement.Id.ToString()) as DirectShapeState).materialId, mat.Id.IntegerValue); BoundingBoxCentroid(ds).DistanceTo(Point.Origin()).ShouldBeApproximately(0); mesh.Dispose(); }
public MeshBinder(Autodesk.Dynamo.MeshToolkit.Mesh mesh, Color color) { dynamoMesh = false; StartingPositions = mesh.Vertices().ToTriples().ToArray(); Color = color; faceIndices = mesh.VertexIndicesByTri(); int faceCount = faceIndices.Count / 3; faces = new IndexGroup[faceCount]; for (int i = 0; i < faceCount; i++) { faces[i] = IndexGroup.ByIndices( (uint)faceIndices[i * 3], (uint)faceIndices[i * 3 + 1], (uint)faceIndices[i * 3 + 2]); } meshIndices = new IntCollection(); foreach (IndexGroup face in faces) { meshIndices.Add((int)face.A); meshIndices.Add((int)face.B); meshIndices.Add((int)face.C); } }
public static Autodesk.DesignScript.Geometry.Mesh ToProtoType(this Autodesk.Revit.DB.Mesh mesh) { var pts = mesh.Vertices.Select(x => x.ToPoint()).ToArray(); var tris = Enumerable.Range(0, mesh.NumTriangles) .Select(mesh.get_Triangle) .Select(tri => IndexGroup.ByIndices(tri.get_Index(0), tri.get_Index(1), tri.get_Index(2))) .ToArray(); return(Autodesk.DesignScript.Geometry.Mesh.ByPointsFaceIndices(pts, tris)); }
public static Mesh CreateMeshBarFromPoints(Point startPoint, Point endPoint, double radius = 8, int edgeCount = 6) { Line distance = Line.ByStartPointEndPoint(startPoint, endPoint); Vector tangentVector = distance.Direction; Circle startCircle = Circle.ByCenterPointRadiusNormal(startPoint, radius, tangentVector); Circle endCircle = Circle.ByCenterPointRadiusNormal(endPoint, radius, tangentVector); List <Point> vertexList = new List <Point>(); List <IndexGroup> indiceList = new List <IndexGroup>(); for (int i = 0; i < edgeCount; i++) //start circle { vertexList.Add(startCircle.PointAtParameter(i / (double)edgeCount)); //not casting to double will fail the division silently returning 0 } for (int i = 0; i < edgeCount; i++) //end circle { vertexList.Add(endCircle.PointAtParameter(i / (double)edgeCount)); } for (int i = 0; i < edgeCount; i++)//facets { if (i < edgeCount - 1) { indiceList.Add(IndexGroup.ByIndices((uint)i, (uint)i + 1, (uint)edgeCount + (uint)i + 1)); //clockwise orientation of vertices [startCircle[0], startCircle[1], endCircle[0], indiceList.Add(IndexGroup.ByIndices((uint)edgeCount + (uint)i + 1, (uint)edgeCount + (uint)i, //clockwise orientation of vertices [endCircle[1], endCircle[0], startCircle[0], (uint)i)); } else if (i == edgeCount - 1) //stitch last strip to first { indiceList.Add(IndexGroup.ByIndices((uint)i, (uint)0, (uint)edgeCount + (uint)0)); indiceList.Add(IndexGroup.ByIndices((uint)edgeCount + (uint)0, (uint)edgeCount + (uint)i, (uint)i)); } } Mesh extrudedBar = Mesh.ByPointsFaceIndices(vertexList, indiceList); //cleanup local vars distance.Dispose(); tangentVector.Dispose(); startCircle.Dispose(); endCircle.Dispose(); vertexList.Clear(); indiceList.Clear(); return(extrudedBar); }
internal static Mesh MTDMeshFromMayaMesh(MFnMesh mayaMesh, MSpace.Space space) { PointList vertices = new PointList(mayaMesh.numVertices); ; List <IndexGroup> faceIndexList = new List <IndexGroup>(mayaMesh.numPolygons); MPointArray mayaVerts = new MPointArray(); mayaMesh.getPoints(mayaVerts, space); if (MGlobal.isYAxisUp) { vertices.AddRange(mayaVerts.Select(v => Point.ByCoordinates(v.x, -v.z, v.y))); } else { vertices.AddRange(mayaVerts.Select(v => Point.ByCoordinates(v.x, v.y, v.z))); } MIntArray faceIndex = new MIntArray(); for (int i = 0; i < mayaMesh.numPolygons; i++) { mayaMesh.getPolygonVertices(i, faceIndex); if (faceIndex.length > 4) { WarningException wa = new WarningException("The DynMesh will not show in Dynamo if it has any faces with 4 verts or more. The DynMesh can be represented as closed curves ."); return(null); } if (faceIndex.length == 3) { faceIndexList.Add(IndexGroup.ByIndices((uint)faceIndex[0], (uint)faceIndex[1], (uint)faceIndex[2])); } else { faceIndexList.Add(IndexGroup.ByIndices((uint)faceIndex[0], (uint)faceIndex[1], (uint)faceIndex[2], (uint)faceIndex[3])); } } mayaMesh.Dispose(); mayaVerts.Dispose(); faceIndex.Dispose(); using (vertices) { return(Mesh.ByPointsFaceIndices(vertices, faceIndexList)); } }
internal DynamoPipeConverter() { //conversion of strings AddConverter(new PipeConverter <string, PipeString>( (str) => { return(new PipeString(str)); }, (pStr) => { return(pStr.Value); } )); //conversion of integers AddConverter(new PipeConverter <int, PipeInteger>( (i) => { return(new PipeInteger(i)); }, (pi) => { return(pi.Value); } )); //conversion of doubles AddConverter(new PipeConverter <double, PipeNumber>( (val) => { return(new PipeNumber(val)); }, (pval) => { return(pval.Value); } )); //conversion of geometry var geomConv = new GeometryConverter(); AddConverter(geomConv); //mesh converter AddConverter(new PipeConverter <Mesh, ppg.Mesh>( (dm) => { return(new ppg.Mesh(dm.VertexPositions.Select((v) => geomConv.ptConv.ToPipe <Point, ppg.Vec>(v)).ToList(), dm.FaceIndices.Select((f) => { if (f.Count == 3) { return new ulong[] { f.A, f.B, f.C }; } else if (f.Count == 4) { return new ulong[] { f.A, f.B, f.C, f.D }; } else { throw new ArgumentException("A Mesh face can only have either 3 or 4 vertices!"); } }).ToList())); }, (ppm) => { return(Mesh.ByPointsFaceIndices(ppm.Vertices.Select((v) => geomConv.ptConv.FromPipe <Point, ppg.Vec>(v)), ppm.Faces.Select((f) => ppg.Mesh.FaceIsTriangle(f) ? IndexGroup.ByIndices((uint)f[0], (uint)f[1], (uint)f[2]) : IndexGroup.ByIndices((uint)f[0], (uint)f[1], (uint)f[2], (uint)f[3])))); } )); }
public static IList <GeometryObject> ToRevitType( this Autodesk.DesignScript.Geometry.Mesh mesh, TessellatedShapeBuilderTarget target = TessellatedShapeBuilderTarget.Mesh, TessellatedShapeBuilderFallback fallback = TessellatedShapeBuilderFallback.Salvage, ElementId MaterialId = null, bool performHostUnitConversion = true) { var verts = mesh.VertexPositions; var indices = mesh.FaceIndices; var tsb = new TessellatedShapeBuilder() { Fallback = fallback, Target = target, GraphicsStyleId = ElementId.InvalidElementId }; tsb.OpenConnectedFaceSet(false); for (int faceindex = 0, count = indices.Count(); faceindex < count; faceindex++) { var f = indices[faceindex]; //if this is a quad face triangulate it if (f.Count > 3) { //and add two triangles to the tessellated shape builder var tri1 = IndexGroup.ByIndices(f.B, f.C, f.A); var tri2 = IndexGroup.ByIndices(f.A, f.C, f.D); AddFace(tsb, tri1, verts, performHostUnitConversion, MaterialId); AddFace(tsb, tri2, verts, performHostUnitConversion, MaterialId); } else { AddFace(tsb, f, verts, performHostUnitConversion, MaterialId); } } tsb.CloseConnectedFaceSet(); tsb.Build(); var result = tsb.GetBuildResult(); foreach (IDisposable vert in verts) { vert.Dispose(); } return(result.GetGeometricalObjects()); }
private static DirectShape CreateDirectShapeFromQuadPoints(Point p1, Point p2, Point p3, Point p4) { var index = IndexGroup.ByIndices(0, 1, 2, 3); var mesh = Mesh.ByPointsFaceIndices(new List <Point>() { p1, p2, p3, p4 }, new List <IndexGroup>() { index }); var mat = DocumentManager.Instance.ElementsOfType <Autodesk.Revit.DB.Material>().First(); var ds = DirectShape.ByMesh(mesh, Category.ByName("OST_GenericModel"), Material.ByName(mat.Name), "a mesh"); mesh.Dispose(); return(ds); }
public static Autodesk.DesignScript.Geometry.Mesh MeshByFacet3dList(List <Facet3d> facets) { List <Point> points = new List <Point>(); List <IndexGroup> groups = new List <IndexGroup>(); uint facetNum = 0; foreach (Facet3d facet in facets) { uint idx = facetNum * 3; points.Add(PointByPoint3d(facet.P0)); points.Add(PointByPoint3d(facet.P1)); points.Add(PointByPoint3d(facet.P2)); groups.Add(IndexGroup.ByIndices(idx, idx + 1, idx + 2)); ++facetNum; } return(Autodesk.DesignScript.Geometry.Mesh.ByPointsFaceIndices(points, groups)); }
/// <summary> /// Join a list of meshes into a single mesh /// </summary> /// <param name="meshes">Meshes to Join</param> /// <returns name="Mesh">Returns a single joined mesh </returns> public static Mesh MeshJoin(List <Mesh> meshes) { // Create lists to store the vertex points and index groups (faces) List <Point> vertices = new List <Point>(); List <IndexGroup> faces = new List <IndexGroup>(); foreach (Mesh m in meshes) { // Get the number of vertices before adding the new set to them // This count will be what we add to the mesh face indices to shift // it to the growing list of vertices. uint currentVertCount = (uint)vertices.Count; // Add the current meshes vertices to the list vertices.AddRange(m.VertexPositions); // Iterate through the mesh faces, creating new ones with shifted indices. foreach (IndexGroup f in m.FaceIndices) { // Create a new Mesh Face (IndexGroup) by adding the vertex count to each index IndexGroup updatedFace = null; if (f.Count == 3) { updatedFace = IndexGroup.ByIndices(f.A + currentVertCount, f.B + currentVertCount, f.C + currentVertCount); } else { updatedFace = IndexGroup.ByIndices(f.A + currentVertCount, f.B + currentVertCount, f.C + currentVertCount, f.D + currentVertCount); } // Add the mesh face to our growing list of mesh faces. faces.Add(updatedFace); } } // Make sure we retrieved data before trying to build a new mesh if (vertices.Count > 0 && faces.Count > 0) { // Build a new mesh with all of the vertices and faces. Mesh mesh = Mesh.ByPointsFaceIndices(vertices, faces); return(mesh); } return(null); }
/// <summary> /// Converts a Parasite Mesh to a Dynamo Mesh /// </summary> /// <param name="mesh">Parasite mesh object </param> /// <returns>A Dynamo Mesh object</returns> public static Autodesk.DesignScript.Geometry.Mesh ToDynamoType(Parasite_Mesh mesh) { int[][] faceIndexes = mesh.FaceIndexes; Parasite_Point3d[] verticesTemp = mesh.Vertices; List <IndexGroup> indexGroups = new List <IndexGroup>(); List <Autodesk.DesignScript.Geometry.Point> vertices = verticesTemp.Select(x => ToDynamoType(x)).ToList(); for (int i = 0; i < faceIndexes.Length; i++) { for (int j = 0; j < faceIndexes[i].Length; j++) { if (faceIndexes[i].Length == 3) { IndexGroup ig = IndexGroup.ByIndices((uint)faceIndexes[i][0], (uint)faceIndexes[i][1], (uint)faceIndexes[i][2]); indexGroups.Add(ig); } if (faceIndexes[i].Length == 4) { IndexGroup ig = IndexGroup.ByIndices((uint)faceIndexes[i][0], (uint)faceIndexes[i][1], (uint)faceIndexes[i][2], (uint)faceIndexes[i][3]); indexGroups.Add(ig); } if (faceIndexes[i].Length > 4 || faceIndexes[i].Length < 3) { throw new ParasiteArgumentException("A Dynamo mesh cant have a face with more than 4 vertices or less than 3"); } } } Autodesk.DesignScript.Geometry.Mesh dMesh = Autodesk.DesignScript.Geometry.Mesh.ByPointsFaceIndices(vertices, indexGroups); // TODO: Check for mesh validity return(dMesh); }
public void ByMeshAndBySurfaceBothLocatedSameMetric() { TransactionManager.Instance.EnsureInTransaction(DocumentManager.Instance.CurrentDBDocument); DocumentManager.Instance.CurrentDBDocument.SetUnits(new Autodesk.Revit.DB.Units(Autodesk.Revit.DB.UnitSystem.Metric)); TransactionManager.Instance.TransactionTaskDone(); var p1 = Point.ByCoordinates(0.0, 0.0, 0.0); var p2 = Point.ByCoordinates(1.0, 1.0, 0); var p3 = Point.ByCoordinates(2.0, 0, 0); var index1 = IndexGroup.ByIndices(0, 1, 2); var mesh = Mesh.ByPointsFaceIndices(new List <Point>() { p1, p2, p3 }, new List <IndexGroup>() { index1 }); var surf = Surface.ByPerimeterPoints(new List <Point>() { p1, p2, p3 }); var mat = DocumentManager.Instance.ElementsOfType <Autodesk.Revit.DB.Material>().First(); var ds = DirectShape.ByMesh(mesh, Category.ByName("OST_GenericModel"), Material.ByName(mat.Name), "a mesh"); var dsSurf = DirectShape.ByGeometry(surf, Category.ByName("OST_GenericModel"), Material.ByName(mat.Name), "a surf"); BoundingBoxCentroid(ds).DistanceTo(BoundingBoxCentroid(dsSurf)).ShouldBeApproximately(0); Surface.ByPerimeterPoints((ds.Geometry().First() as Mesh).VertexPositions).Area.ShouldDifferByLessThanPercentage( (dsSurf.Geometry().First() as Surface).Area, ApproximateAssertExtensions.Epsilon); mesh.Dispose(); surf.Dispose(); }
/// <summary> /// This method just creates a 10x10x10 cube and outputs it if the bool input is set to true. /// This was just a simple test to see if it would show up. /// </summary> public static Autodesk.DesignScript.Geometry.Mesh GetSimpleMesh(bool returnBox) { // Build vertex list List <Autodesk.DesignScript.Geometry.Point> vertices = new List <Autodesk.DesignScript.Geometry.Point>(); vertices.Add(Autodesk.DesignScript.Geometry.Point.ByCoordinates(0, 0, 0)); vertices.Add(Autodesk.DesignScript.Geometry.Point.ByCoordinates(10, 0, 0)); vertices.Add(Autodesk.DesignScript.Geometry.Point.ByCoordinates(10, 10, 0)); vertices.Add(Autodesk.DesignScript.Geometry.Point.ByCoordinates(0, 10, 0)); vertices.Add(Autodesk.DesignScript.Geometry.Point.ByCoordinates(0, 0, 10)); vertices.Add(Autodesk.DesignScript.Geometry.Point.ByCoordinates(10, 0, 10)); vertices.Add(Autodesk.DesignScript.Geometry.Point.ByCoordinates(10, 10, 10)); vertices.Add(Autodesk.DesignScript.Geometry.Point.ByCoordinates(0, 10, 10)); // build mesh faces List <IndexGroup> faces = new List <IndexGroup>(); faces.Add(IndexGroup.ByIndices(3, 2, 1, 0)); faces.Add(IndexGroup.ByIndices(4, 5, 1, 0)); faces.Add(IndexGroup.ByIndices(5, 6, 2, 1)); faces.Add(IndexGroup.ByIndices(6, 7, 3, 2)); faces.Add(IndexGroup.ByIndices(7, 4, 0, 3)); faces.Add(IndexGroup.ByIndices(7, 6, 5, 4)); // create the mesh Autodesk.DesignScript.Geometry.Mesh mesh = Autodesk.DesignScript.Geometry.Mesh.ByPointsFaceIndices(vertices, faces); if (returnBox) { return(mesh); } else { return(null); } }
public static List <List <Object> > DisplayLoads(StructuralModel StructuralModel, string LPattern = "Show All", double Size = 1.0, bool ShowValues = true, double TextSize = 1.0) { //List to hold all the load visualization objects List <List <Object> > LoadViz = new List <List <Object> >(); // Length of the arrow Double length = 1.0; foreach (Element e in StructuralModel.StructuralElements) { if (e.GetType().ToString().Contains("Frame")) { //get the length of the first frame found in the collection and use to set up a scale for the load display length = ((Frame)e).BaseCrv.Length; break; } } double max = -10000000.0; double min = 10000000.0; // Loop through all the elements in the structural model // get the max and minimum values of distributed loads on the frame foreach (Element e in StructuralModel.StructuralElements) { // If the object is a frame if (e.GetType().ToString().Contains("Frame")) { Frame f = e as Frame; if (f.Loads != null && f.Loads.Count > 0) { foreach (Load load in f.Loads) { if (load.LoadType == "DistributedLoad") { if (load.Val > max) { max = load.Val; } if (load.Val < min) { min = load.Val; } if (load.Val2 > max) { max = load.Val2; } if (load.Val2 < min) { min = load.Val2; } } } } } } double refval = Math.Abs(max); if (Math.Abs(max) < Math.Abs(min)) { refval = Math.Abs(min); } // Loop through all the elements in the structural model foreach (Element e in StructuralModel.StructuralElements) { //List to hold the load visualization objects per structural member List <Object> LoadObjects = new List <Object>(); // 1. If the object is a frame if (e.GetType().ToString().Contains("Frame")) { Frame f = e as Frame; if (f.Loads != null && f.Loads.Count > 0) { //Loop through all the loads on the frame foreach (Load load in f.Loads) { // If the Load type is a moment, throw warning if (load.FMType == 2) { throw new Exception("Moment visualization is not supported"); } Point labelLocation = null; if (LPattern == "Show All") { //show all } else { if (load.lPattern.name != LPattern) { continue; // show only the loads whose load pattern is the specified in the node } } Curve c = f.BaseCrv; double sz = Size; // List to hold parameter values where arrows will be drawn List <double> dd = new List <double>(); bool isDistributed = false; if (load.LoadType == "DistributedLoad") { isDistributed = true; //number of arrows to represent a Distributed Load int n = Convert.ToInt32((load.Dist - load.Dist2) / 0.1); double step = (load.Dist - load.Dist2) / n; for (double i = load.Dist; i < load.Dist2; i += step) { dd.Add(i); } dd.Add(load.Dist2); } else // if its a point load { dd.Add(load.Dist); } //First top point for distributed load visualization Point A = null; //Last top point for distributed load visualization Point B = null; //Vector used to translate the arrow location point Vector v = null; Vector xAxis = c.TangentAtParameter(0.0); Vector yAxis = c.NormalAtParameter(0.0); Vector triangleNormal = null; //List to hold the index group of the mesh List <IndexGroup> igs = new List <IndexGroup>(); igs.Add(IndexGroup.ByIndices(0, 1, 2)); double arrowLenght = length / 6; double triangleBase = arrowLenght / 5; //Loop through all the parameter values along the curve. // If it is a point load it will only have one value for (int i = 0; i < dd.Count; i++) { //List to hold the points to create a triangular mesh per arrow List <Point> pps = new List <Point>(); //Create the point where the arrow should be located Point p1 = c.PointAtParameter(dd[i]); Point p2 = null; Point p3 = null; Point p4 = null; arrowLenght = -length / 6; double b = load.Val; if (load.Dist != 0) { b = load.Val - (load.Val2 - load.Val) / (load.Dist2 - load.Dist) * dd[0]; } double valueAtd = ((load.Val2 - load.Val) / (load.Dist2 - load.Dist)) * dd[i] + b; if (isDistributed) { arrowLenght *= valueAtd / refval; } //Calculate the vector needed to create the line of the arrow // if it's the local X Direction if (load.Dir == 1) { v = xAxis; } // if it's the local Y Direction else if (load.Dir == 2) { v = yAxis; } // if it's the local Z Direction else if (load.Dir == 3) { v = xAxis.Cross(yAxis); } // if it's the global X Direction else if (load.Dir == 4) { v = Vector.ByCoordinates(arrowLenght * sz, 0.0, 0.0); } // if it's the global Y Direction else if (load.Dir == 5) { v = Vector.ByCoordinates(0.0, arrowLenght * sz, 0.0); } // if it's the global Z Direction else if (load.Dir == 6) { v = Vector.ByCoordinates(0.0, 0.0, arrowLenght * sz); } // if the direction is 7, 8, 9, 10 or 11 else { throw new Exception("The direction of some of the loads is not supported"); } if (Math.Round(v.Length, 3) != 0.0) { // Create the line of the arrow p2 = (Point)p1.Translate(v); Line ln = Line.ByStartPointEndPoint(p1, p2); // Create a temporary point to hold the position of the base of the triangle of the arrow arrowLenght = length / 6; Point ptOnArrow = ln.PointAtDistance(arrowLenght / 5); triangleNormal = ln.Normal; // Translate the point on the arrow to the sides to create the base of the triangle p3 = (Point)ptOnArrow.Translate(triangleNormal, triangleBase); p4 = (Point)ptOnArrow.Translate(triangleNormal, -triangleBase); // Add the points to the list pps.Add(p1); pps.Add(p3); pps.Add(p4); //Create the triangular mesh of the arrow Mesh m = Mesh.ByPointsFaceIndices(pps, igs); //Add the arrow objects to the list LoadObjects.Add(ln); LoadObjects.Add(m); } // Calculate the location of the labels if (isDistributed) { //if it is the start value if (i == 0) { if (p2 == null) { A = p1; } else { A = p2; } if (load.Val != load.Val2) { if (Math.Round(v.Length, 3) != 0.0) { if (ShowValues) { labelLocation = (Point)A.Translate(v.Normalized().Scale(arrowLenght / 4)); labelLocation.Translate(triangleNormal, 2 * triangleBase); string value = Math.Round(load.Val, 2).ToString(); // value of the load rounded to two decimals createLabel(LoadObjects, labelLocation, value, TextSize); } } } } //if it is the end value else if (i == dd.Count - 1) { if (p2 == null) { B = p1; } else { B = p2; } //If it is a distributed load, create a top line Line topLine = Line.ByStartPointEndPoint(A, B); LoadObjects.Add(topLine); if (load.Val != load.Val2) { if (Math.Round(v.Length, 3) != 0.0) { if (ShowValues) { labelLocation = (Point)B.Translate(v.Normalized().Scale(arrowLenght / 4)); labelLocation.Translate(triangleNormal, -2 * triangleBase); string value = Math.Round(load.Val2, 2).ToString(); // value of the load rounded to two decimals createLabel(LoadObjects, labelLocation, value, TextSize); } } } } else if (i == Convert.ToInt32(dd.Count / 2) && load.Val == load.Val2) // if it is the middle point of a uniform distributed load { labelLocation = (Point)p2.Translate(v.Normalized().Scale(arrowLenght / 4)); if (ShowValues) { string value = Math.Round(load.Val, 2).ToString(); // value of the load rounded to two decimals createLabel(LoadObjects, labelLocation, value, TextSize); } } } //if it is a pointLoad else { // If the user wants to see the values of the forces if (ShowValues) { labelLocation = (Point)p2.Translate(v.Normalized().Scale(arrowLenght / 4)); string value = Math.Round(load.Val, 2).ToString(); // value of the load rounded to two decimals createLabel(LoadObjects, labelLocation, value, TextSize); } } } } } } //TO DO: // 2. Add condition for cable // 3. Add contiditon for shell LoadViz.Add(LoadObjects); } // Return Load Visualization return(LoadViz); }
/// <summary> /// Visualize forces and moments in the model for a specific case /// </summary> /// <param name="StructuralModel">Structural model to visualize results on</param> /// <param name="AnalysisResults">Use Analysis.Run and Analysis.GetResults to select a specific case to decompose</param> /// <param name="ForceType">Use Force Type dropdown</param> /// <param name="Scale">Scale of the visualization</param> /// <param name="Visualize">Set Boolean to True to draw the meshes</param> /// <returns>Forces and moments in the form of meshes for each station in each structural member in the model</returns> /// public static List <List <Mesh> > VisualizeResults(StructuralModel StructuralModel, Analysis AnalysisResults, string ForceType, bool Visualize, double Scale = 1.0) { List <List <double> > myForces = DecomposeResults(StructuralModel, AnalysisResults, ForceType); double max = 0.0; for (int i = 0; i < myForces.Count; i++) { for (int j = 0; j < myForces[i].Count; j++) { if (myForces[i][j] >= max) { max = myForces[i][j]; } } } // Define a coefficient to visualize the forces Frame fs = (Frame)StructuralModel.StructuralElements[0]; double lenght = 0.5 * fs.BaseCrv.Length; double coefficient = 0.0; if (max != 0) { coefficient = lenght / max; } List <List <Mesh> > VizMeshes = new List <List <Mesh> >(); List <Line> frameNormals = new List <Line>(); if (Visualize) { int j = 0; for (int i = 0; i < StructuralModel.StructuralElements.Count; i++) //for (int i = 0; i < AnalysisResults.FrameResults.Count; i++) { //List of meshes per structural element in the model List <Mesh> frameResultsMesh = new List <Mesh>(); // Linq inqury FrameResults frmresult = null; try { frmresult = (from frm in AnalysisResults.FrameResults where frm.ID == StructuralModel.StructuralElements[i].Label select frm).First(); } catch { } if (frmresult == null || StructuralModel.StructuralElements[i].Type != Structure.Type.Frame) { //Add an empty list to match the tree that contains Joints VizMeshes.Add(frameResultsMesh); continue; } // Get the frame's curve specified by the frameID // Frame f = (Frame)StructuralModel.StructuralElements[i]; Frame f = (Frame)(from frame in StructuralModel.StructuralElements where frame.Label == frmresult.ID select frame).First(); Curve c = f.BaseCrv; //LOCAL COORDINATE SYSTEM Vector xAxis = c.TangentAtParameter(0.0); Vector yAxis = c.NormalAtParameter(0.0); //This ensures the right axis for the Z direction CoordinateSystem localCS = CoordinateSystem.ByOriginVectors(c.StartPoint, xAxis, yAxis); //LINES TO VISUALIZE THE NORMALS OF THE FRAME CURVES //Point middlePt = c.PointAtParameter(0.5); //Line ln = Line.ByStartPointDirectionLength(middlePt, localCS.ZAxis, 30.0); //frameNormals.Add(ln); //List to hold the points to make a mesh face List <Point> MeshPoints = new List <Point>(); // t value of the previous station double t1 = 0.0; // t value of the current station double t2 = 0.0; // Local Z value of the previous station double v1 = 0.0; // Local Z value of the current station double v2 = 0; // Integer to count the number of times there are stations with value=0 (no force) int zeroCount = 0; // Loop through each station (t value) in the analysis results dictionary foreach (double t in frmresult.Results[AnalysisResults.LCaseOrLPatternRun].Keys) { double newt = t; if (t < 0) { newt = -t; } Mesh m = null; Point tPoint = c.PointAtParameter(newt); // Point on the curve at the specified t2 Point vPoint = null; // Point that is translated from the cPoint according to the value v2 // Double to hold the local Z value of the station multiplied by the scale factor double translateCoord = 0.0; if (ForceType == "Axial") // Get Axial P { v2 = AnalysisResults.FrameResults[j].Results[AnalysisResults.LCaseOrLPatternRun][newt].P; translateCoord = v2 * coefficient * (-Scale); } else if (ForceType == "Shear22") // Get Shear V2 { v2 = AnalysisResults.FrameResults[j].Results[AnalysisResults.LCaseOrLPatternRun][newt].V2; translateCoord = v2 * coefficient * (-Scale); } else if (ForceType == "Shear33") // Get Shear V3 { v2 = AnalysisResults.FrameResults[j].Results[AnalysisResults.LCaseOrLPatternRun][newt].V3; translateCoord = v2 * coefficient * coefficient * Scale; } else if (ForceType == "Torsion") // Get Torsion T { v2 = AnalysisResults.FrameResults[j].Results[AnalysisResults.LCaseOrLPatternRun][newt].T; translateCoord = v2 * coefficient * (-Scale); } else if (ForceType == "Moment22") // Get Moment M2 { v2 = AnalysisResults.FrameResults[j].Results[AnalysisResults.LCaseOrLPatternRun][newt].M2; translateCoord = v2 * coefficient * (-Scale); } else if (ForceType == "Moment33") // Get Moment M3 { v2 = AnalysisResults.FrameResults[j].Results[AnalysisResults.LCaseOrLPatternRun][newt].M3; translateCoord = v2 * coefficient * Scale; } v2 = Math.Round(v2, 4, MidpointRounding.AwayFromZero); // if there is no value for the force (it is zero), add one to the count if (translateCoord == 0.0) { zeroCount++; } if (ForceType == "Moment22" || ForceType == "Shear33") { vPoint = (Point)tPoint.Translate(localCS.YAxis, translateCoord); // Translate in the Y direction to match the visualization of SAP } else { vPoint = (Point)tPoint.Translate(localCS.ZAxis, translateCoord); // All the other types must be translate in the Z direction} } //Point that results from the intersection of the line between two value Points and the frame curve Point pzero = null; IndexGroup ig3 = IndexGroup.ByIndices(0, 1, 2); IndexGroup ig4 = IndexGroup.ByIndices(0, 1, 2, 3); //if no points have been added yet, add the point on the curve and then the point representing the value if (MeshPoints.Count == 0) { MeshPoints.Add(tPoint); //index 0 // if the first value is not 0 if (v2 != 0.0) { MeshPoints.Add(vPoint); //index 1 } } // if a previous point(s) has been added else { // List to hold the indices for the mesh face List <IndexGroup> indices = new List <IndexGroup>(); //Parameter at which the value of the forces = 0. It is the X coordinate of the pzero double tzero; // Current t parameter of the point being visualized relative to the length of the frame curve t2 = newt * c.Length; // If there is a change in the force sign, calculate the intersection point // Then, add two trianglular mesh faces if ((v1 > 0 && v2 < 0) || (v1 < 0 && v2 > 0)) { // The function of the line is: y= (t2-t1)tzero/(d2-d1)+d1 This has to be equal to 0 double ml = (v2 - v1) / (t2 - t1); tzero = (0 - v1) / ml; // Add the X coordinate of the last mesh point tzero += t1; pzero = Point.ByCartesianCoordinates(localCS, tzero, 0.0, 0.0); //Add the third point for the first triangular mesh face MeshPoints.Add(pzero); //index 2 indices.Add(ig3); // ||| Color coding here m = Mesh.ByPointsFaceIndices(MeshPoints, indices); frameResultsMesh.Add(m); MeshPoints.Clear(); //Add the third point for the second triangular mesh face MeshPoints.Add(pzero); //new face index 0 // Add the current station's points MeshPoints.Add(tPoint); //new face index 1 MeshPoints.Add(vPoint); //new face index 2 // ||| Color coding here m = Mesh.ByPointsFaceIndices(MeshPoints, indices); frameResultsMesh.Add(m); } // Create a quad mesh face or a triangular mesh if the first value was 0 else { MeshPoints.Add(vPoint); //index 2 (note: vPoint before cPoint) MeshPoints.Add(tPoint); //index 3 if (MeshPoints.Count == 4) { indices.Add(ig4); } else { indices.Add(ig3); } // ||| Color coding here m = Mesh.ByPointsFaceIndices(MeshPoints, indices); frameResultsMesh.Add(m); } //Clear the temporary list MeshPoints.Clear(); // Add the current station's points MeshPoints.Add(tPoint); //new face index 0 MeshPoints.Add(vPoint); //new face index 1 } // Update the values for the next station t1 = newt * c.Length; v1 = v2; } // If all the values were zero, show empty list in output for that specific member if (zeroCount == AnalysisResults.FrameResults[j].Results[AnalysisResults.LCaseOrLPatternRun].Keys.Count) { frameResultsMesh.Clear(); } VizMeshes.Add(frameResultsMesh); j++; } } return(VizMeshes); }
private static Autodesk.DesignScript.Geometry.Mesh SolidToMesh(Autodesk.Revit.DB.Solid solid) { Autodesk.DesignScript.Geometry.Mesh mesh = null; List <Autodesk.DesignScript.Geometry.Mesh> unjoinedMeshes = new List <Autodesk.DesignScript.Geometry.Mesh>(); foreach (Autodesk.Revit.DB.Face f in solid.Faces) { Autodesk.Revit.DB.Mesh rMesh = f.Triangulate(); Autodesk.DesignScript.Geometry.Mesh dMesh = RevitToProtoMesh.ToProtoType(rMesh, true); unjoinedMeshes.Add(dMesh); } // Join meshes if (unjoinedMeshes.Count == 0) { mesh = unjoinedMeshes[0]; } else { // Join all of the meshes? List <Autodesk.DesignScript.Geometry.Point> vertices = new List <Autodesk.DesignScript.Geometry.Point>(); List <IndexGroup> indexGroups = new List <IndexGroup>(); foreach (Autodesk.DesignScript.Geometry.Mesh m in unjoinedMeshes) { if (m == null) { continue; } int baseCount = vertices.Count; foreach (Autodesk.DesignScript.Geometry.Point pt in m.VertexPositions) { vertices.Add(pt); } foreach (IndexGroup ig in m.FaceIndices) { if (ig.Count == 3) { IndexGroup iGroup = IndexGroup.ByIndices((uint)(ig.A + baseCount), (uint)(ig.B + baseCount), (uint)(ig.C + baseCount)); indexGroups.Add(iGroup); } else { IndexGroup iGroup = IndexGroup.ByIndices((uint)(ig.A + baseCount), (uint)(ig.B + baseCount), (uint)(ig.C + baseCount), (uint)(ig.D + baseCount)); indexGroups.Add(iGroup); } } } try { Autodesk.DesignScript.Geometry.Mesh joinedMesh = Autodesk.DesignScript.Geometry.Mesh.ByPointsFaceIndices(vertices, indexGroups); if (joinedMesh != null) { mesh = joinedMesh; simpleMeshes.Add(joinedMesh); } } catch { // For now just add them all as is } } return(mesh); }