private IEnumerable <GeometryObject> Tessellate(Curve curve)
        {
            var result = new List <GeometryObject>();

            // use the ASM tesselation of the curve
            var pkg = renderPackageFactory.CreateRenderPackage();

            curve.Tessellate(pkg, renderPackageFactory.TessellationParameters);

            // get necessary info to enumerate and convert the lines
            //var lineCount = pkg.LineVertexCount * 3 - 3;
            var verts = pkg.LineStripVertices.ToList();

            // we scale the tesselation rather than the curve
            var conv = UnitConverter.DynamoToHostFactor(UnitType.UT_Length);

            var scaledXYZs = new List <XYZ>();

            for (var i = 0; i < verts.Count; i += 3)
            {
                scaledXYZs.Add(new XYZ(verts[i] * conv, verts[i + 1] * conv, verts[i + 2] * conv));
            }
            result.Add(PolyLine.Create(scaledXYZs));

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Tessellate the curve:
        /// 1). If there are more than 2 points, create a polyline out of the points;
        /// 2). If there are exactly 2 points, create a line;
        /// 3). If there's exception thrown during the tessellation process, attempt to create
        /// a line from start and end points. If that fails, a point will be created instead.
        /// </summary>
        /// <param name="curve"></param>
        /// <returns></returns>
        private IEnumerable <GeometryObject> Tessellate(Curve curve)
        {
            var result = new List <GeometryObject>();

            try
            {
                // we scale the tesselation rather than the curve
                var conv = UnitConverter.DynamoToHostFactor(UnitType.UT_Length);

                // use the ASM tesselation of the curve
                var pkg = renderPackageFactory.CreateRenderPackage();
                curve.Tessellate(pkg, renderPackageFactory.TessellationParameters);

                // get necessary info to enumerate and convert the lines
                //var lineCount = pkg.LineVertexCount * 3 - 3;
                var verts = pkg.LineStripVertices.ToList();

                if (verts.Count > 2)
                {
                    var scaledXYZs = new List <XYZ>();
                    for (var i = 0; i < verts.Count; i += 3)
                    {
                        scaledXYZs.Add(new XYZ(verts[i] * conv, verts[i + 1] * conv, verts[i + 2] * conv));
                    }
                    result.Add(PolyLine.Create(scaledXYZs));
                }
                else if (verts.Count == 2)
                {
                    result.Add(Line.CreateBound(curve.StartPoint.ToXyz(), curve.EndPoint.ToXyz()));
                }
            }
            catch (Exception)
            {
                // Add a red bounding box geometry to identify that some errors occur
                var bbox = curve.BoundingBox;
                result.AddRange(ProtoToRevitMesh.CreateBoundingBoxMeshForErrors(bbox.MinPoint, bbox.MaxPoint));

                try
                {
                    result.Add(Line.CreateBound(curve.StartPoint.ToXyz(), curve.EndPoint.ToXyz()));
                }
                catch (Exception)
                {
                    try
                    {
                        result.Add(DocumentManager.Instance.CurrentUIApplication.Application.Create.NewPoint(curve.StartPoint.ToXyz()));
                    }
                    catch (ArgumentException)
                    {
                        //if either the X, Y or Z of the point is infinite, no need to add it for preview
                    }
                }
            }

            return(result);
        }