protected static int ToPointsBuffer
        (
            Rhino.Geometry.Point point,
            out VertexFormatBits vertexFormatBits,
            out VertexBuffer vb, out int vertexCount,
            out IndexBuffer ib
        )
        {
            int pointsCount = 0;

            if (point.Location.IsValid)
            {
                pointsCount = 1;
                vertexCount = 1;

                vertexFormatBits = VertexFormatBits.Position;
                vb = new VertexBuffer(pointsCount * VertexPosition.GetSizeInFloats());
                vb.Map(pointsCount * VertexPosition.GetSizeInFloats());
                using (var vstream = vb.GetVertexStreamPosition())
                {
                    vstream.AddVertex(new VertexPosition(RawEncoder.ToHost(point.Location)));
                }
                vb.Unmap();

                ib = IndexPointsBuffer(pointsCount);
            }
            else
            {
                vertexFormatBits = 0;
                vb = null; vertexCount = 0;
                ib = null;
            }

            return(pointsCount);
        }
        public void SendTest()
        {
            RawEncoder encoder = new RawEncoder();

            byte[] content = { 0x01, 0x02, 0x03, 0x04 };

            using (MemoryStream stream = new MemoryStream())
            {
                // The encoder should write the content 'as is' to the stream.
                encoder.Send(stream, new VncPixelFormat(), new VncRectangle(), content);

                Assert.Equal(content, stream.ToArray());
            }
        }
        protected static int ToPolylineBuffer
        (
            Rhino.Geometry.Polyline polyline,
            out VertexFormatBits vertexFormatBits,
            out VertexBuffer vb, out int vertexCount,
            out IndexBuffer ib
        )
        {
            int linesCount = 0;

            if (polyline.SegmentCount > 0)
            {
                linesCount  = polyline.SegmentCount;
                vertexCount = polyline.Count;

                vertexFormatBits = VertexFormatBits.Position;
                vb = new VertexBuffer(vertexCount * VertexPosition.GetSizeInFloats());
                vb.Map(vertexCount * VertexPosition.GetSizeInFloats());
                using (var vstream = vb.GetVertexStreamPosition())
                {
                    foreach (var v in polyline)
                    {
                        vstream.AddVertex(new VertexPosition(RawEncoder.ToHost(v)));
                    }
                }
                vb.Unmap();

                ib = IndexLinesBuffer(vertexCount);
            }
            else
            {
                vertexFormatBits = 0;
                vb = null; vertexCount = 0;
                ib = null;
            }

            return(linesCount);
        }
Exemple #4
0
        //internal static Brep ToRawBrep(/*const*/ Brep brep, double scaleFactor)
        //{
        //    brep = brep.DuplicateShallow() as Brep;
        //    return EncodeRaw(ref brep, scaleFactor) ? brep : default;
        //}

        //internal static bool EncodeRaw(ref Brep brep, double scaleFactor)
        //{
        //    if (scaleFactor != 1.0 && !brep.Scale(scaleFactor))
        //        return default;

        //    var bbox = brep.GetBoundingBox(false);
        //    if (!bbox.IsValid || bbox.Diagonal.Length < Revit.ShortCurveTolerance)
        //        return default;

        //    return SplitFaces(ref brep);
        //}

        //static bool SplitFaces(ref Brep brep)
        //{
        //    Brep brepToSplit = null;
        //    while (!ReferenceEquals(brepToSplit, brep))
        //    {
        //        brepToSplit = brep;

        //        foreach (var face in brepToSplit.Faces)
        //        {
        //            var splitters = new List<Curve>();

        //            var trimsBBox = BoundingBox.Empty;
        //            foreach (var trim in face.OuterLoop.Trims)
        //                trimsBBox.Union(trim.GetBoundingBox(true));

        //            var domainUV = new Interval[]
        //            {
        //    new Interval(trimsBBox.Min.X, trimsBBox.Max.X),
        //    new Interval(trimsBBox.Min.Y, trimsBBox.Max.Y),
        //            };

        //            // Compute splitters
        //            var splittedUV = new bool[2] { false, false };
        //            for (int d = 0; d < 2; d++)
        //            {
        //                var domain = domainUV[d];
        //                var t = domain.Min;

        //                while (face.GetNextDiscontinuity(d, Continuity.Gsmooth_continuous, t, domain.Max, out t))
        //                {
        //                    splitters.AddRange(face.TrimAwareIsoCurve(1 - d, t));
        //                    splittedUV[d] = true;
        //                }
        //            }

        //            var closedUV = new bool[2] { face.IsClosed(0), face.IsClosed(1) };
        //            if (!splittedUV[0] && closedUV[0])
        //            {
        //                splitters.AddRange(face.TrimAwareIsoCurve(1, face.Domain(0).Mid));
        //                splittedUV[0] = true;
        //            }
        //            if (!splittedUV[1] && closedUV[1])
        //            {
        //                splitters.AddRange(face.TrimAwareIsoCurve(0, face.Domain(1).Mid));
        //                splittedUV[1] = true;
        //            }

        //            if (splitters.Count > 0)
        //            {
        //                var surfaceIndex = face.SurfaceIndex;
        //                var splitted = face.Split(splitters, Revit.ShortCurveTolerance);
        //                if (splitted is null)
        //                {
        //                    Debug.Fail("BrepFace.Split", "Failed to split a closed face.");
        //                    return false;
        //                }

        //                if (brepToSplit.Faces.Count == splitted.Faces.Count)
        //                {
        //                    // Split was ok but for tolerance reasons no new faces were created.
        //                    // Too near from the limits.
        //                }
        //                else
        //                {

        //                    foreach (var f in splitted.Faces)
        //                    {
        //                        if (f.SurfaceIndex != surfaceIndex)
        //                            continue;

        //                        if (splittedUV[0] && splittedUV[1])
        //                            f.ShrinkFace(BrepFace.ShrinkDisableSide.ShrinkAllSides);
        //                        else if (splittedUV[0])
        //                            f.ShrinkFace(BrepFace.ShrinkDisableSide.DoNotShrinkSouthSide | BrepFace.ShrinkDisableSide.DoNotShrinkNorthSide);
        //                        else if (splittedUV[1])
        //                            f.ShrinkFace(BrepFace.ShrinkDisableSide.DoNotShrinkEastSide | BrepFace.ShrinkDisableSide.DoNotShrinkWestSide);
        //                    }

        //                    // Start again until no face is splitted
        //                    brep = splitted;
        //                    break;
        //                }
        //            }
        //        }
        //    }

        //    return brep is object;
        //}
        #endregion

        #region Transfer
        /// <summary>
        /// Replaces <see cref="Raw.RawEncoder.ToHost(Brep)"/> to catch Revit Exceptions
        /// </summary>
        /// <param name="brep"></param>
        /// <returns></returns>
        public static NXOpen.Body ToSolid(/*const*/ Brep brep)
        {
            try
            {
                BodyStyle brepType;
                switch (brep.SolidOrientation)
                {
                case BrepSolidOrientation.Inward: brepType = BodyStyle.Sheet; break;

                case BrepSolidOrientation.Outward: brepType = BodyStyle.Solid; break;
                }

                List <NXOpen.Body> faceBodies = new List <NXOpen.Body>();
                foreach (var face in brep.Faces)
                {
                    using (var nurbSurface = face.ToNurbsSurface())
                    {
                        List <(List <NXOpen.Curve> edgeCurves, List <int> directions)> boundaryEdgeCurves = new List <(List <NXOpen.Curve>, List <int>)>();
                        for (int i = 0; i < face.Loops.Count; i++)
                        {
                            List <NXOpen.Curve> currentEdgeCurves = new List <NXOpen.Curve>();

                            List <int> currentDirections = new List <int>();

                            var trims = face.Loops[i].Trims;
                            for (int j = 0; j < trims.Count; j++)
                            {
                                if (trims[j].TrimType != BrepTrimType.Boundary && trims[j].TrimType != BrepTrimType.Mated)
                                {
                                    continue;
                                }

                                BrepEdge edge = trims[j].Edge;

                                if (edge is null)
                                {
                                    continue;
                                }

                                var currentEdgeCurve = edge.Trim(edge.Domain).ToCurve();

                                currentEdgeCurves.Add(currentEdgeCurve);
                                currentDirections.Add(edge.ProxyCurveIsReversed ? -1 : 1);
                            }

                            boundaryEdgeCurves.Add((currentEdgeCurves, currentDirections));
                        }

                        if (nurbSurface.IsPlanar())
                        {
                            // 使用有界平面创建
                            var boundaryCurves   = boundaryEdgeCurves.SelectMany(obj => obj.edgeCurves).ToArray();
                            var boundedPlaneBody = _theSession.Parts.Work.Features.CreateBoundedPlane(boundaryCurves).GetBodies()[0];
                            boundedPlaneBody.RemoveParameter();
                            faceBodies.Add(boundedPlaneBody);
                            //boundaryCurves.Delete();

                            //NXOpen.UF.StringList bplaneBoundaries = new NXOpen.UF.StringList()
                            //{
                            //    num = boundaryEdgeCurves.Count,
                            //    id = boundaryEdgeCurves.SelectMany(obj => obj.edgeCurveTags).ToArray(),
                            //    dir = boundaryEdgeCurves.SelectMany(obj => obj.directions).ToArray(),
                            //    _string = boundaryEdgeCurves.Select(obj => obj.edgeCurveTags.Count).ToArray()
                            //};

                            //double[] tolerrances = new double[bplaneBoundaries.dir.Length];
                            //for (int i = 0; i < tolerrances.Length; i++)
                            //    tolerrances[i] = DistanceTolerance;
                            //theUfSession.Modl.CreateBplane(ref bplaneBoundaries, tolerrances, out NXOpen.Tag body);

                            //bplaneBoundaries.id.Select(obj => obj.GetTaggedObject() as NXOpen.NXObject).ToArray().Delete();
                        }
                        else
                        {
                            var faceBody = RawEncoder.ToHost(nurbSurface);

                            #region 找到一个位于面上的点
                            Point3d point3dOnFace     = new Point3d();
                            bool    findInteriorPoint = false;
                            if (face.IsPointOnFace(face.Domain(0).Mid, face.Domain(1).Mid) == PointFaceRelation.Interior)
                            {
                                face.Evaluate(face.Domain(0).Mid, face.Domain(1).Mid, 0, out point3dOnFace, out _);
                                findInteriorPoint = true;
                            }
                            else
                            {
                                var firstEdge = face.Loops.ElementAt(0).Trims.ElementAt(0).Edge;

                                Point3d ptOnFirstEdge = firstEdge.PointAt(firstEdge.Domain.Mid);

                                face.ClosestPoint(ptOnFirstEdge, out double u, out double v);

                                double offsetValue = 0.02;

                                u += offsetValue;
                                v += offsetValue;
                                if (face.IsPointOnFace(u, v) == PointFaceRelation.Interior)
                                {
                                    face.Evaluate(u, v, 0, out point3dOnFace, out _);
                                    findInteriorPoint = true;
                                }
                                else
                                {
                                    u -= 2 * offsetValue;
                                    v -= 2 * offsetValue;
                                    if (face.IsPointOnFace(u, v) == PointFaceRelation.Interior)
                                    {
                                        face.Evaluate(u, v, 0, out point3dOnFace, out _);
                                        findInteriorPoint = true;
                                    }
                                }
                            }

                            if (!findInteriorPoint)
                            {
                                findInteriorPoint.ToString().ListingWindowWriteLine();
                                var copiedFaceBody = faceBody.CopyAndPaste()[0] as NXOpen.Body;
                                copiedFaceBody.SetColor(216);
                            }
                            #endregion

                            for (int i = 0; i < boundaryEdgeCurves.Count; i++)
                            {
                                NXOpen.UF.UFModl.TrimObject[] trimBoundaryObjects = new NXOpen.UF.UFModl.TrimObject[boundaryEdgeCurves[i].edgeCurves.Count];

                                for (int j = 0; j < trimBoundaryObjects.Length; j++)
                                {
                                    trimBoundaryObjects[j] = new NXOpen.UF.UFModl.TrimObject {
                                        object_tag = boundaryEdgeCurves[i].edgeCurves[j].Tag, curve_project_method = boundaryEdgeCurves[i].directions[j]
                                    };
                                }

                                _theUfSession.Modl.TrimSheet(faceBody.Tag, trimBoundaryObjects.Length, trimBoundaryObjects, null, 1, 1, point3dOnFace.ToXYZ().ToArray(), 0.01, out int numGapPoints, out double[] gapPoints);
                            }

                            faceBodies.Add(faceBody);
                        }
#if !DEBUG
                        boundaryEdgeCurves.SelectMany(obj => obj.edgeCurves).ToArray().Delete();
#endif
                    }
                }

                NXOpen.Body resultBody = null;
                if (faceBodies.Count > 1)
                {
                    var sewFeature = WorkPart.Features.CreateSew(faceBodies.ToArray());
                    resultBody = sewFeature.GetBodies()[0];
#if !DEBUG
                    resultBody.RemoveParameter();
#endif
                }
                else
                {
                    resultBody = faceBodies[0];
                }

                var brepArea = brep.GetArea();
                var bodyArea = resultBody.GetArea();
                resultBody.SetUserAttribute("Area", -1, bodyArea, NXOpen.Update.Option.Now);
                var error = Math.Abs(brepArea - bodyArea) / brepArea;
                resultBody.SetUserAttribute("Error", -1, error, NXOpen.Update.Option.Now);
                if (error > 0.001)
                {
                    resultBody.SetColor(186);
                }
            }
            catch (NXOpen.NXException e)
            {
                Logger.Error(e.ToString());
            }

            return(null);
        }
        protected static int ToPointsBuffer
        (
            Rhino.Geometry.PointCloud pointCloud,
            Primitive.Part part,
            out VertexFormatBits vertexFormatBits,
            out VertexBuffer vb, out int vertexCount,
            out IndexBuffer ib
        )
        {
            int pointsCount = part.VertexCount;
            int normalCount = pointCloud.ContainsNormals ? pointsCount : 0;
            int colorsCount = pointCloud.ContainsColors  ? pointsCount : 0;

            bool hasPoints  = pointsCount > 0;
            bool hasNormals = normalCount == pointsCount;
            bool hasColors  = colorsCount == pointsCount;

            if (hasPoints)
            {
                if (hasNormals)
                {
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormalColored;
                        vb = new VertexBuffer(pointsCount * VertexPositionNormalColored.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPositionNormalColored.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPositionNormalColored())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                var c     = new ColorWithTransparency(point.Color.R, point.Color.G, point.Color.B, 255u - point.Color.A);
                                vstream.AddVertex(new VertexPositionNormalColored(RawEncoder.ToHost(point.Location), RawEncoder.ToHost(point.Normal), c));
                            }
                        }

                        vb.Unmap();
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormal;
                        vb = new VertexBuffer(pointsCount * VertexPositionNormal.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPositionNormal.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPositionNormal())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                vstream.AddVertex(new VertexPositionNormal(RawEncoder.ToHost(point.Location), RawEncoder.ToHost(point.Normal)));
                            }
                        }

                        vb.Unmap();
                    }
                }
                else
                {
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionColored;
                        vb = new VertexBuffer(pointsCount * VertexPositionColored.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPositionColored.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPositionColored())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                var c     = new ColorWithTransparency(point.Color.R, point.Color.G, point.Color.B, 255u - point.Color.A);
                                vstream.AddVertex(new VertexPositionColored(RawEncoder.ToHost(point.Location), c));
                            }
                        }

                        vb.Unmap();
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.Position;
                        vb = new VertexBuffer(pointsCount * VertexPosition.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPosition.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPosition())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                vstream.AddVertex(new VertexPosition(RawEncoder.ToHost(point.Location)));
                            }
                        }

                        vb.Unmap();
                    }
                }

                ib = IndexPointsBuffer(pointsCount);
            }
            else
            {
                vertexFormatBits = 0;
                vb = null;
                ib = null;
            }

            vertexCount = pointsCount;
            return(pointsCount);
        }
        protected static VertexBuffer ToVertexBuffer
        (
            Rhino.Geometry.Mesh mesh,
            Primitive.Part part,
            out VertexFormatBits vertexFormatBits,
            System.Drawing.Color color = default
        )
        {
            int verticesCount = part.EndVertexIndex - part.StartVertexIndex;
            int normalCount   = mesh.Normals.Count == mesh.Vertices.Count ? verticesCount : 0;
            int colorsCount   = color.IsEmpty ? (mesh.VertexColors.Count == mesh.Vertices.Count ? verticesCount : 0) : verticesCount;

            bool hasVertices = verticesCount > 0;
            bool hasNormals  = normalCount > 0;
            bool hasColors   = colorsCount > 0;

            if (hasVertices)
            {
                var vertices = mesh.Vertices;
                if (hasNormals)
                {
                    var normals = mesh.Normals;
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormalColored;
                        var colors = mesh.VertexColors;
                        var vb     = new VertexBuffer(verticesCount * VertexPositionNormalColored.GetSizeInFloats());
                        vb.Map(verticesCount * VertexPositionNormalColored.GetSizeInFloats());
                        using (var stream = vb.GetVertexStreamPositionNormalColored())
                        {
                            for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v)
                            {
                                var  c = !color.IsEmpty ? color : colors[v];
                                uint T = Math.Max(1, 255u - c.A);
                                stream.AddVertex(new VertexPositionNormalColored(RawEncoder.ToHost(vertices[v]), RawEncoder.ToHost(normals[v]), new ColorWithTransparency(c.R, c.G, c.B, T)));
                            }
                        }
                        vb.Unmap();
                        return(vb);
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormal;
                        var vb = new VertexBuffer(verticesCount * VertexPositionNormal.GetSizeInFloats());
                        vb.Map(verticesCount * VertexPositionNormal.GetSizeInFloats());
                        using (var stream = vb.GetVertexStreamPositionNormal())
                        {
                            for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v)
                            {
                                stream.AddVertex(new VertexPositionNormal(RawEncoder.ToHost(vertices[v]), RawEncoder.ToHost(normals[v])));
                            }
                        }
                        vb.Unmap();
                        return(vb);
                    }
                }
                else
                {
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionColored;
                        var colors = mesh.VertexColors;
                        var vb     = new VertexBuffer(verticesCount * VertexPositionColored.GetSizeInFloats());
                        vb.Map(verticesCount * VertexPositionColored.GetSizeInFloats());
                        using (var stream = vb.GetVertexStreamPositionColored())
                        {
                            for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v)
                            {
                                var  c = !color.IsEmpty ? color : colors[v];
                                uint T = Math.Max(1, 255u - c.A);
                                stream.AddVertex(new VertexPositionColored(RawEncoder.ToHost(vertices[v]), new ColorWithTransparency(c.R, c.G, c.B, T)));
                            }
                        }
                        vb.Unmap();
                        return(vb);
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.Position;
                        var vb = new VertexBuffer(verticesCount * VertexPosition.GetSizeInFloats());
                        vb.Map(verticesCount * VertexPosition.GetSizeInFloats());
                        using (var stream = vb.GetVertexStreamPosition())
                        {
                            for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v)
                            {
                                stream.AddVertex(new VertexPosition(RawEncoder.ToHost(vertices[v])));
                            }
                        }
                        vb.Unmap();
                        return(vb);
                    }
                }
            }

            vertexFormatBits = 0;
            return(null);
        }
        public void EncodingTest()
        {
            var encoder = new RawEncoder();

            Assert.Equal(VncEncoding.Raw, encoder.Encoding);
        }
Exemple #8
0
 public static DB.Arc ToHost(this ArcCurve curve) => RawEncoder.ToHost(curve);
Exemple #9
0
 public static DB.Line ToHost(this LineCurve curve) => RawEncoder.ToHost(curve);
Exemple #10
0
 public static IEnumerable <DB.XYZ> ToHost(this IEnumerable <Point3d> points) => points.Select(p => RawEncoder.ToHost(p));