/// <summary>
            /// align the label geometry to the face that it represents
            /// </summary>
            /// <param name="geo"></param>
            /// <returns></returns>
            private IEnumerable <Curve> AlignGeoToFace(IEnumerable <Curve> geo)
            {
                var oldgeo          = new List <DesignScriptEntity>();
                var approxCenter    = Tesselation.MeshHelpers.SurfaceAsPolygonCenter(UnfoldableFace.SurfaceEntities.First());
                var norm            = UnfoldableFace.SurfaceEntities.First().NormalAtPoint(approxCenter);
                var facePlane       = Plane.ByOriginNormal(approxCenter, norm);
                var finalCordSystem = CoordinateSystem.ByPlane(facePlane);

                //a list for collecting old geo that needs to be disposed
                oldgeo.Add(facePlane);
                // find bounding box of set of curves
                var textBoudingBox = BoundingBox.ByGeometry(geo);

                oldgeo.Add(textBoudingBox);
                // find the center of this box and use as start point
                var textCeneter = textBoudingBox.MinPoint.Add((
                                                                  textBoudingBox.MaxPoint.Subtract(textBoudingBox.MinPoint.AsVector())
                                                                  .AsVector().Scale(.5)));

                var transVector = Vector.ByTwoPoints(textCeneter, Point.ByCoordinates(0, 0, 0));

                var geoIntermediateTransform = geo.Select(x => x.Translate(transVector)).Cast <Curve>().AsEnumerable();

                oldgeo.AddRange(geoIntermediateTransform);
                var finalTransformedLabel = geoIntermediateTransform.Select(x => x.Transform(finalCordSystem)).Cast <Curve>().AsEnumerable();

                foreach (IDisposable item in oldgeo)
                {
                    item.Dispose();
                }

                return(finalTransformedLabel);
            }
Beispiel #2
0
        public static void AssertLabelsGoodStartingLocationAndOrientation <K, T>(List <PlanarUnfolder.UnfoldableFaceLabel
                                                                                       <K, T> > labels)
            where K : IUnfoldableEdge
            where T : IUnfoldablePlanarFace <K>
        {
            // get aligned geometry
            var alignedGeo = labels.Select(x => x.AlignedLabelGeometry).ToList();

            // assert that the bounding box of the label at least intersects the face it represents
            for (int i = 0; i < alignedGeo.Count; i++)
            {
                var curveList = alignedGeo[i];

                var curvePoints = curveList.Select(x => x.StartPoint);
                var labelPlane  = Plane.ByBestFitThroughPoints(curvePoints);
                var testsurf    = Surface.ByPatch(Rectangle.ByWidthLength(1, 1).
                                                  Transform(CoordinateSystem.ByPlane(labelPlane)) as Curve);

                //check that the aligned curves intersect the surface they are aligned to
                Assert.IsTrue(curveList.SelectMany(x => labels[i].UnfoldableFace.SurfaceEntities.Select(x.DoesIntersect)).Any());
                Console.WriteLine("This label was in the right spot at the start of the unfold");
                //also assert that the face normal is parallel with the normal of the boundingbox plane

                var face = labels[i].UnfoldableFace.SurfaceEntities;

                UnfoldTestUtils.AssertSurfacesAreCoplanar(testsurf, face.First());
                Console.WriteLine("This label was in the right orientation at the start of the unfold");

                testsurf.Dispose();
                labelPlane.Dispose();
            }
        }
Beispiel #3
0
        private static void CreatePlaneTessellation(IRenderPackage package, Plane plane)
        {
            package.RequiresPerVertexColoration = true;

            var s = 2.5;

            var cs = CoordinateSystem.ByPlane(plane);
            var a  = Point.ByCartesianCoordinates(cs, s, s, 0);
            var b  = Point.ByCartesianCoordinates(cs, -s, s, 0);
            var c  = Point.ByCartesianCoordinates(cs, -s, -s, 0);
            var d  = Point.ByCartesianCoordinates(cs, s, -s, 0);

            //Add two triangles to represent the plane
            package.AddTriangleVertex(a.X, a.Y, a.Z);
            package.AddTriangleVertex(b.X, b.Y, b.Z);
            package.AddTriangleVertex(c.X, c.Y, c.Z);

            package.AddTriangleVertex(c.X, c.Y, c.Z);
            package.AddTriangleVertex(d.X, d.Y, d.Z);
            package.AddTriangleVertex(a.X, a.Y, a.Z);

            //Add the mesh vertex UV, normal, and color data for the 6 triangle vertices
            for (var i = 0; i < 6; i++)
            {
                package.AddTriangleVertexUV(0, 0);
                package.AddTriangleVertexNormal(plane.Normal.X, plane.Normal.Y, plane.Normal.Z);
                package.AddTriangleVertexColor(0, 0, 0, 10);
            }

            // Draw plane edges
            package.AddLineStripVertex(a.X, a.Y, a.Z);
            package.AddLineStripVertex(b.X, b.Y, b.Z);
            package.AddLineStripVertex(b.X, b.Y, b.Z);
            package.AddLineStripVertex(c.X, c.Y, c.Z);
            package.AddLineStripVertex(c.X, c.Y, c.Z);
            package.AddLineStripVertex(d.X, d.Y, d.Z);
            package.AddLineStripVertex(d.X, d.Y, d.Z);
            package.AddLineStripVertex(a.X, a.Y, a.Z);

            // Draw normal
            package.AddLineStripVertex(plane.Origin.X, plane.Origin.Y, plane.Origin.Z);
            var nEnd = plane.Origin.Add(plane.Normal.Scale(2.5));

            package.AddLineStripVertex(nEnd.X, nEnd.Y, nEnd.Z);

            //Add the line vertex data for the plane line geometry (4 plane edges and 1 normal).
            for (var i = 0; i < 5; i++)
            {
                package.AddLineStripVertexCount(2);
                package.AddLineStripVertexColor(MidTone, MidTone, MidTone, 255);
                package.AddLineStripVertexColor(MidTone, MidTone, MidTone, 255);
            }

            //dispose helper geometry
            a.Dispose();
            b.Dispose();
            c.Dispose();
            d.Dispose();
            cs.Dispose();
        }
Beispiel #4
0
        /// <summary>
        /// This method returns a coordinateSystem from the TransformedAlignedCutPlane, this is useful for visualization
        /// The coordinateSystems will appear at the origin since they are transformed using the inverse
        /// </summary>
        /// <returns></returns>
        public CoordinateSystem AlignedCoordinateSystemAtOrigin([DefaultArgumentAttribute("Vector.ByCoordinates(1,0,0)")] Vector alignTo)
        {
            var plane  = this.AlignedCutPlaneAtOrigin(alignTo);
            var output = CoordinateSystem.ByPlane(plane);

            plane.Dispose();
            return(output);
        }
Beispiel #5
0
        /// <summary>
        /// align the label geometry to the solid that it represents
        /// </summary>
        /// <param name="geo"></param>
        /// <returns></returns>
        private IEnumerable <Curve> AlignGeoToSolid(IEnumerable <Curve> geo)
        {
            var oldgeo          = new List <IDisposable>();
            var approxCenter    = AttachedGeo.GeometryToLabel.Centroid();
            var norm            = Vector.ZAxis();
            var solidBB         = AttachedGeo.GeometryToLabel.BoundingBox;
            var newpoint        = approxCenter.Translate(0, 0, solidBB.MaxPoint.Z - solidBB.MinPoint.Z) as Point;
            var plane           = Plane.ByOriginNormal(newpoint, norm);
            var finalCordSystem = CoordinateSystem.ByPlane(plane);

            oldgeo.Add(approxCenter);
            oldgeo.Add(norm);
            oldgeo.Add(solidBB);
            oldgeo.Add(plane);
            oldgeo.Add(newpoint);
            //oldgeo.Add(finalCordSystem);

            // find bounding box of set of curves
            var textBoudingBox = BoundingBox.ByGeometry(geo);

            oldgeo.Add(textBoudingBox);
            // find the center of this box and use as start point
            var textmin    = textBoudingBox.MinPoint;
            var textmax    = textBoudingBox.MaxPoint;
            var textminvec = textmin.AsVector();

            var textCenter = textmin.Add((
                                             textmax.Subtract(textminvec))
                                         .AsVector().Scale(.5));

            oldgeo.Add(textBoudingBox);
            oldgeo.Add(textCenter);
            oldgeo.Add(textmin);
            oldgeo.Add(textmax);
            oldgeo.Add(textminvec);


            var transVector = Vector.ByTwoPoints(textCenter, Point.ByCoordinates(0, 0, 0));

            var geoIntermediateTransform = geo.Select(x => x.Translate(transVector)).Cast <Curve>().AsEnumerable();

            oldgeo.AddRange(geoIntermediateTransform);
            //oldgeo.Add(transVector);

            var finalTransformedLabel = geoIntermediateTransform.Select(x => x.Transform(finalCordSystem)).Cast <Curve>();

            foreach (IDisposable item in oldgeo)
            {
                item.Dispose();
            }

            return(finalTransformedLabel);
        }
Beispiel #6
0
        //align all surfaces down, generate new transforms
        //
        private static Tuple <List <T>, List <PlanarUnfolder.FaceTransformMap> > MoveSurfacesInUnfoldToPlane <K, T>(PlanarUnfolder.PlanarUnfolding <K, T> unfold)
            where K : IUnfoldableEdge
            where T : IUnfoldablePlanarFace <K>, new()
        {
            var alignDownTransforms = new List <PlanarUnfolder.FaceTransformMap>();
            var translatedFaces     = new List <T>();
            var faces = unfold.UnfoldedFaces;


            foreach (var facelike in faces)
            {
                var surfaceToAlignDown = facelike.SurfaceEntities;

                // get the coordinate system defined by the face normal
                var somePointOnSurface = facelike.SurfaceEntities.First().PointAtParameter(.5, .5);
                var norm             = facelike.SurfaceEntities.First().NormalAtParameter(.5, .5);
                var facePlane        = Plane.ByOriginNormal(somePointOnSurface, norm);
                var startCoordSystem = CoordinateSystem.ByPlane(facePlane);
                //TODO need to cleanup planes, coord systems...

                // transform surface to horizontal plane at x,y,0 of org surface
                var tempSurfaces = surfaceToAlignDown.Select(x => x.Transform(startCoordSystem,
                                                                              CoordinateSystem.ByPlane(Plane.ByOriginXAxisYAxis(
                                                                                                           Point.ByCoordinates(somePointOnSurface.X, somePointOnSurface.Y, 0),
                                                                                                           Vector.XAxis(), Vector.YAxis()))) as Surface).ToList();

                var flatcoordsystem = CoordinateSystem.ByPlane(Plane.ByOriginXAxisYAxis(
                                                                   Point.ByCoordinates(somePointOnSurface.X, somePointOnSurface.Y, 0),
                                                                   Vector.XAxis(), Vector.YAxis()));
                // save transformation for each set, this should have all the ids present
                //TODO is this incorrect? should we be saving tempSurfaces?
                alignDownTransforms.Add(new PlanarUnfolder.FaceTransformMap(startCoordSystem, flatcoordsystem, facelike.IDS));

                //alignDownTransforms.Add(new PlanarUnfolder.FaceTransformMap(
                //startCoordSystem, facelike.IDS));

                //create a new facelike to hold the new surfaces that are aligned to the plane
                //the ids are the same though which lets us label these and apply the correct transformations
                var newfacelike = new T();
                newfacelike.OriginalEntity  = facelike.OriginalEntity;
                newfacelike.SurfaceEntities = tempSurfaces;
                newfacelike.ID  = facelike.ID;
                newfacelike.IDS = facelike.IDS;
                newfacelike.EdgeLikeEntities = facelike.EdgeLikeEntities;

                translatedFaces.Add(newfacelike);
            }
            return(Tuple.Create(translatedFaces, alignDownTransforms));
        }
Beispiel #7
0
        public static Dictionary <string, List <List <Object> > > getLinesCoordSysAtPoint(List <Line> lines, List <Point> points)
        {
            List <List <Object> > lineListList = new List <List <Object> >();
            List <List <Object> > CSListList   = new List <List <Object> >();
            List <Point>          errPoints    = new List <Point>();

            foreach (Point p in points)
            {
                List <Object> lineList = new List <Object>();
                List <Object> csList   = new List <Object>();

                List <bool> boolList = new List <bool>();

                foreach (Line l in lines)
                {
                    bool intersection = p.DoesIntersect(l);
                    if (intersection)
                    {
                        bool meets = l.StartPoint.IsAlmostEqualTo(p);
                        if (meets)
                        {
                            lineList.Add(l);
                            Plane            pl = l.PlaneAtParameter(0);
                            CoordinateSystem cs = CoordinateSystem.ByPlane(pl);
                            csList.Add((Object)cs);
                        }
                        else
                        {
                            Line lRev = (Line)l.Reverse();
                            lineList.Add((Object)lRev);
                            Plane            pl = lRev.PlaneAtParameter(0);
                            CoordinateSystem cs = CoordinateSystem.ByPlane(pl);
                            csList.Add((Object)cs);
                        }
                    }
                }

                lineListList.Add(lineList);
                CSListList.Add(csList);
            }

            return(new Dictionary <string, List <List <Object> > >
            {
                { "lineList", lineListList },
                { "planeList", CSListList }
            });
        }
Beispiel #8
0
        //////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////



        /// <summary>
        /// Create geometry representation of tool.
        /// </summary>
        /// <param name="frame">Tool frame at drill tip</param>
        /// <returns></returns>
        private static Solid vis_tool(Plane frame)
        {
            List <Solid>     model = new List <Solid>();
            CoordinateSystem cs    = CoordinateSystem.ByPlane(frame);
            Vector           x     = frame.XAxis;
            Vector           y     = frame.YAxis;
            Vector           z     = frame.Normal.Reverse();
            double           h     = frame.Origin.Z;

            Solid bit0 = Cone.ByCoordinateSystemHeightRadii(cs.Translate(z, 2), 2, 3, 0.1);
            Solid bit1 = Cone.ByCoordinateSystemHeightRadii(cs.Translate(z, 68 + 2), 68, 3, 3);
            Solid bit2 = Cone.ByCoordinateSystemHeightRadii(cs.Translate(z, 5 + 68 + 2), 5, 5, 5);
            Solid bit3 = Cone.ByCoordinateSystemHeightRadii(cs.Translate(z, 5 + 5 + 68 + 2), 5, 4, 4);
            Solid bod0 = Cone.ByCoordinateSystemHeightRadii(cs.Translate(z, 3 + 5 + 5 + 68 + 2), 3, 12, 12);
            Solid bod1 = Cone.ByCoordinateSystemHeightRadii(cs.Translate(z, 80 + 3 + 5 + 5 + 68 + 2), 80, 24, 24);
            Solid bod2 = Cone.ByCoordinateSystemHeightRadii(cs.Translate(z, 80 + 80 + 3 + 5 + 5 + 68 + 2), 80, 34, 34);
            Solid bod3 = Cuboid.ByLengths(cs.Translate(z, 40 + 80 + 3 + 5 + 5 + 68 + 2).Translate(y, 20), 40, 40, 40);
            Solid leg0 = Cone.ByCoordinateSystemHeightRadii(cs.Translate(z, h).Translate(x, 40).Translate(y, 40), h - (80 + 3 + 5 + 5 + 68 + 2), 4, 4);
            Solid leg1 = Cone.ByCoordinateSystemHeightRadii(cs.Translate(z, h).Translate(x, 40).Translate(y, -40), h - (80 + 3 + 5 + 5 + 68 + 2), 4, 4);
            Solid leg2 = Cone.ByCoordinateSystemHeightRadii(cs.Translate(z, h).Translate(x, -40).Translate(y, -40), h - (80 + 3 + 5 + 5 + 68 + 2), 4, 4);
            Solid leg3 = Cone.ByCoordinateSystemHeightRadii(cs.Translate(z, h).Translate(x, -40).Translate(y, 40), h - (80 + 3 + 5 + 5 + 68 + 2), 4, 4);
            Solid box0 = Cuboid.ByLengths(cs.Translate(z, 3 + 80 + 3 + 5 + 5 + 68 + 2), 96, 96, 10);
            Solid box1 = Cuboid.ByLengths(cs.Translate(z, h - 5), 96, 96, 10);

            model.Add(bit0);
            model.Add(bit1);
            model.Add(bit2);
            model.Add(bit3);
            model.Add(bod0);
            model.Add(bod1);
            model.Add(bod2);
            model.Add(bod3);
            model.Add(leg0);
            model.Add(leg1);
            model.Add(leg2);
            model.Add(leg3);
            model.Add(box0);
            model.Add(box1);

            Solid solid = Solid.ByUnion(model);

            return(solid);
        }
Beispiel #9
0
        /// <summary>
        /// Create geometry representation of wobj.
        /// </summary>
        /// <param name="frame">Wobj frame at cube centroid</param>
        /// <returns></returns>
        private static Solid vis_wobj(Plane frame)
        {
            List <Solid>     model = new List <Solid>();
            CoordinateSystem cs    = CoordinateSystem.ByPlane(frame);
            Vector           z     = frame.Normal.Reverse();
            double           h     = frame.Origin.Z;

            //Solid cube = Cuboid.ByLengths(cs, 40, 40, 40);
            Solid chu0 = Cylinder.ByCoordinateSystemHeightRadii(cs.Translate(cs.ZAxis.Reverse(), 2 + 20), 2, 10, 10);
            Solid bod0 = Cylinder.ByCoordinateSystemHeightRadii(cs.Translate(cs.ZAxis.Reverse(), 18 + 2 + 20), 18, 22, 22);
            Solid bod1 = Cuboid.ByLengths(cs.Translate(z, h - 18 - (h - 18 - 18 - 2 - 20) / 2), 46, 46, h - 18 - 18 - 2 - 20);
            Solid bod2 = Cylinder.ByCoordinateSystemHeightRadii(cs.Translate(z, h), 18, 22, 22);

            //model.Add(cube);
            model.Add(chu0);
            model.Add(bod0);
            model.Add(bod1);
            model.Add(bod2);

            Solid solid = Solid.ByUnion(model);

            return(solid);
        }
        private void GetRenderPackagesFromMirrorData(MirrorData mirrorData, bool displayLabels, bool isNodeSelected, ref List <string> labelMap, ref int count)
        {
            if (mirrorData.IsNull)
            {
                return;
            }

            if (mirrorData.IsCollection)
            {
                foreach (var el in mirrorData.GetElements())
                {
                    GetRenderPackagesFromMirrorData(el, displayLabels, isNodeSelected, ref labelMap, ref count);
                }
            }
            else
            {
                var graphicItem = mirrorData.Data as IGraphicItem;
                if (graphicItem == null)
                {
                    return;
                }

                var package = factory.CreateRenderPackage();
                package.Description = labelMap.Count > count ? labelMap[count] : "?";

                try
                {
                    graphicItem.Tessellate(package, factory.TessellationParameters);
                    if (package.MeshVertexColors.Count() > 0)
                    {
                        package.RequiresPerVertexColoration = true;
                    }

                    if (factory.TessellationParameters.ShowEdges)
                    {
                        var surf = graphicItem as Surface;
                        if (surf != null)
                        {
                            foreach (var curve in surf.PerimeterCurves())
                            {
                                curve.Tessellate(package, factory.TessellationParameters);
                                curve.Dispose();
                            }
                        }

                        var solid = graphicItem as Solid;
                        if (solid != null)
                        {
                            var edges = solid.Edges;
                            foreach (var geom in edges.Select(edge => edge.CurveGeometry))
                            {
                                geom.Tessellate(package, factory.TessellationParameters);
                                geom.Dispose();
                            }
                            edges.ForEach(x => x.Dispose());
                        }
                    }

                    var plane = graphicItem as Plane;
                    if (plane != null)
                    {
                        package.RequiresPerVertexColoration = true;

                        var s = 2.5;

                        var cs = CoordinateSystem.ByPlane(plane);
                        var a  = Point.ByCartesianCoordinates(cs, s, s, 0);
                        var b  = Point.ByCartesianCoordinates(cs, -s, s, 0);
                        var c  = Point.ByCartesianCoordinates(cs, -s, -s, 0);
                        var d  = Point.ByCartesianCoordinates(cs, s, -s, 0);

                        // Get rid of the original plane geometry.
                        package.Clear();

                        package.AddTriangleVertex(a.X, a.Y, a.Z);
                        package.AddTriangleVertex(b.X, b.Y, b.Z);
                        package.AddTriangleVertex(c.X, c.Y, c.Z);

                        package.AddTriangleVertex(c.X, c.Y, c.Z);
                        package.AddTriangleVertex(d.X, d.Y, d.Z);
                        package.AddTriangleVertex(a.X, a.Y, a.Z);

                        package.AddTriangleVertexUV(0, 0);
                        package.AddTriangleVertexUV(0, 0);
                        package.AddTriangleVertexUV(0, 0);
                        package.AddTriangleVertexUV(0, 0);
                        package.AddTriangleVertexUV(0, 0);
                        package.AddTriangleVertexUV(0, 0);

                        // Draw plane edges
                        package.AddLineStripVertex(a.X, a.Y, a.Z);
                        package.AddLineStripVertex(b.X, b.Y, b.Z);
                        package.AddLineStripVertex(b.X, b.Y, b.Z);
                        package.AddLineStripVertex(c.X, c.Y, c.Z);
                        package.AddLineStripVertex(c.X, c.Y, c.Z);
                        package.AddLineStripVertex(d.X, d.Y, d.Z);
                        package.AddLineStripVertex(d.X, d.Y, d.Z);
                        package.AddLineStripVertex(a.X, a.Y, a.Z);

                        // Draw normal
                        package.AddLineStripVertex(plane.Origin.X, plane.Origin.Y, plane.Origin.Z);
                        var nEnd = plane.Origin.Add(plane.Normal.Scale(2.5));
                        package.AddLineStripVertex(nEnd.X, nEnd.Y, nEnd.Z);

                        for (var i = 0; i < package.LineVertexCount / 2; i++)
                        {
                            package.AddLineStripVertexCount(2);
                        }

                        for (var i = 0; i < package.LineVertexCount; i++)
                        {
                            package.AddLineStripVertexColor(MidTone, MidTone, MidTone, 255);
                        }

                        for (var i = 0; i < package.MeshVertexCount; i++)
                        {
                            package.AddTriangleVertexNormal(plane.Normal.X, plane.Normal.Y, plane.Normal.Z);
                        }

                        for (var i = 0; i < package.MeshVertexCount; i++)
                        {
                            package.AddTriangleVertexColor(0, 0, 0, 10);
                        }
                    }

                    // The default color coming from the geometry library for
                    // curves is 255,255,255,255 (White). Because we want a default
                    // color of 0,0,0,255 (Black), we adjust the color components here.
                    if (graphicItem is Curve || graphicItem is Surface || graphicItem is Solid || graphicItem is Point)
                    {
                        if (package.LineVertexCount > 0 && package.LineStripVertexColors.Count() <= 0)
                        {
                            package.ApplyLineVertexColors(CreateColorByteArrayOfSize(package.LineVertexCount, DefR, DefG, DefB, DefA));
                        }

                        if (package.PointVertexCount > 0 && package.PointVertexColors.Count() <= 0)
                        {
                            package.ApplyPointVertexColors(CreateColorByteArrayOfSize(package.PointVertexCount, DefR, DefG, DefB, DefA));
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(
                        "PushGraphicItemIntoPackage: " + e);
                }

                package.DisplayLabels = displayLabels;
                package.IsSelected    = isNodeSelected;

                renderPackages.Add(package);
                count++;
            }
        }
Beispiel #11
0
        private void GetRenderPackagesFromMirrorData(MirrorData mirrorData, string tag, bool displayLabels, bool isNodeSelectednt)
        {
            if (mirrorData.IsNull)
            {
                return;
            }

            if (mirrorData.IsCollection)
            {
                int count = 0;
                foreach (var el in mirrorData.GetElements())
                {
                    if (el.IsCollection || el.Data is IGraphicItem)
                    {
                        string newTag = tag + ":" + count;
                        GetRenderPackagesFromMirrorData(el, newTag, displayLabels, isNodeSelected);
                    }
                    count = count + 1;
                }
            }
            else
            {
                var graphicItem = mirrorData.Data as IGraphicItem;
                if (graphicItem == null)
                {
                    return;
                }

                var package = factory.CreateRenderPackage();
                var packageWithTransform = package as ITransformable;
                package.Description = tag;

                try
                {
                    graphicItem.Tessellate(package, factory.TessellationParameters);
                    if (package.MeshVertexColors.Count() > 0)
                    {
                        package.RequiresPerVertexColoration = true;
                    }

                    //If the package has a transform that is not the identity matrix
                    //then set requiresCustomTransform to true.
                    if (packageWithTransform != null && packageWithTransform.Transform.SequenceEqual(
                            new double[] { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }) == false)
                    {
                        (packageWithTransform).RequiresCustomTransform = true;
                    }

                    if (factory.TessellationParameters.ShowEdges)
                    {
                        var topology = graphicItem as Topology;
                        if (topology != null)
                        {
                            var surf = graphicItem as Surface;
                            if (surf != null)
                            {
                                foreach (var curve in surf.PerimeterCurves())
                                {
                                    curve.Tessellate(package, factory.TessellationParameters);
                                    curve.Dispose();
                                }
                            }
                            else
                            {
                                var edges = topology.Edges;
                                foreach (var geom in edges.Select(edge => edge.CurveGeometry))
                                {
                                    geom.Tessellate(package, factory.TessellationParameters);
                                    geom.Dispose();
                                }
                                edges.ForEach(x => x.Dispose());
                            }
                        }
                    }

                    var plane = graphicItem as Plane;
                    if (plane != null)
                    {
                        package.RequiresPerVertexColoration = true;

                        var s = 2.5;

                        var cs = CoordinateSystem.ByPlane(plane);
                        var a  = Point.ByCartesianCoordinates(cs, s, s, 0);
                        var b  = Point.ByCartesianCoordinates(cs, -s, s, 0);
                        var c  = Point.ByCartesianCoordinates(cs, -s, -s, 0);
                        var d  = Point.ByCartesianCoordinates(cs, s, -s, 0);

                        // Get rid of the original plane geometry.
                        package.Clear();

                        package.AddTriangleVertex(a.X, a.Y, a.Z);
                        package.AddTriangleVertex(b.X, b.Y, b.Z);
                        package.AddTriangleVertex(c.X, c.Y, c.Z);

                        package.AddTriangleVertex(c.X, c.Y, c.Z);
                        package.AddTriangleVertex(d.X, d.Y, d.Z);
                        package.AddTriangleVertex(a.X, a.Y, a.Z);

                        package.AddTriangleVertexUV(0, 0);
                        package.AddTriangleVertexUV(0, 0);
                        package.AddTriangleVertexUV(0, 0);
                        package.AddTriangleVertexUV(0, 0);
                        package.AddTriangleVertexUV(0, 0);
                        package.AddTriangleVertexUV(0, 0);

                        // Draw plane edges
                        package.AddLineStripVertex(a.X, a.Y, a.Z);
                        package.AddLineStripVertex(b.X, b.Y, b.Z);
                        package.AddLineStripVertex(b.X, b.Y, b.Z);
                        package.AddLineStripVertex(c.X, c.Y, c.Z);
                        package.AddLineStripVertex(c.X, c.Y, c.Z);
                        package.AddLineStripVertex(d.X, d.Y, d.Z);
                        package.AddLineStripVertex(d.X, d.Y, d.Z);
                        package.AddLineStripVertex(a.X, a.Y, a.Z);

                        // Draw normal
                        package.AddLineStripVertex(plane.Origin.X, plane.Origin.Y, plane.Origin.Z);
                        var nEnd = plane.Origin.Add(plane.Normal.Scale(2.5));
                        package.AddLineStripVertex(nEnd.X, nEnd.Y, nEnd.Z);

                        for (var i = 0; i < package.LineVertexCount / 2; i++)
                        {
                            package.AddLineStripVertexCount(2);
                        }

                        for (var i = 0; i < package.LineVertexCount; i++)
                        {
                            package.AddLineStripVertexColor(MidTone, MidTone, MidTone, 255);
                        }

                        for (var i = 0; i < package.MeshVertexCount; i++)
                        {
                            package.AddTriangleVertexNormal(plane.Normal.X, plane.Normal.Y, plane.Normal.Z);
                        }

                        for (var i = 0; i < package.MeshVertexCount; i++)
                        {
                            package.AddTriangleVertexColor(0, 0, 0, 10);
                        }
                    }

                    // The default color coming from the geometry library for
                    // curves is 255,255,255,255 (White). Because we want a default
                    // color of 0,0,0,255 (Black), we adjust the color components here.
                    if (graphicItem is Curve || graphicItem is Surface || graphicItem is Solid || graphicItem is Point)
                    {
                        if (package.LineVertexCount > 0 && package.LineStripVertexColors.Count() <= 0)
                        {
                            package.ApplyLineVertexColors(CreateColorByteArrayOfSize(package.LineVertexCount, DefR, DefG, DefB, DefA));
                        }

                        if (package.PointVertexCount > 0 && package.PointVertexColors.Count() <= 0)
                        {
                            package.ApplyPointVertexColors(CreateColorByteArrayOfSize(package.PointVertexCount, DefR, DefG, DefB, DefA));
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(
                        "PushGraphicItemIntoPackage: " + e);
                }

                package.DisplayLabels = displayLabels;
                package.IsSelected    = isNodeSelected;

                renderPackages.Add(package);
            }
        }
Beispiel #12
0
        /// <summary>
        /// Gets the rectangular openings.
        /// </summary>
        /// <returns></returns>
        /// <remarks>This method uses walls, doors, windows and generic models bounding boxes to determine the rectangles.
        /// These objects can be in the host file or in linked Revit files.</remarks>
        public static IList <Autodesk.DesignScript.Geometry.Rectangle> GetRectangularOpenings()
        {
            Utils.Log(string.Format("OpeningUtils.GetRectangularOpenings started...", ""));

            Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;

            //look for Walls, Doors, Windows, Generic Models in the current document and in the linked documents
            ElementCategoryFilter wallFilter    = new ElementCategoryFilter(BuiltInCategory.OST_Walls);
            ElementCategoryFilter doorFilter    = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
            ElementCategoryFilter windowFilter  = new ElementCategoryFilter(BuiltInCategory.OST_Windows);
            ElementCategoryFilter genericFilter = new ElementCategoryFilter(BuiltInCategory.OST_GenericModel);

            IList <ElementFilter> filterList = new List <ElementFilter>()
            {
                wallFilter, doorFilter, windowFilter, genericFilter
            };

            LogicalOrFilter orFilter = new LogicalOrFilter(filterList);

            IList <Autodesk.DesignScript.Geometry.Solid> solids = new List <Autodesk.DesignScript.Geometry.Solid>();

            IList <Autodesk.DesignScript.Geometry.Rectangle> output = new List <Autodesk.DesignScript.Geometry.Rectangle>();

            foreach (Autodesk.Revit.DB.Element e in new FilteredElementCollector(doc)
                     .WherePasses(orFilter)
                     .WhereElementIsNotElementType()
                     .Where(x =>
                            x.Parameters.Cast <Autodesk.Revit.DB.Parameter>()
                            .First(p => p.Id.IntegerValue == (int)BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).HasValue))
            {
                string comments = e.Parameters.Cast <Autodesk.Revit.DB.Parameter>().First(p => p.Id.IntegerValue == (int)BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).AsString();

                if (comments.ToLower() != "opening")
                {
                    continue;
                }

                Transform tr = Transform.Identity;

                if (e is Instance)
                {
                    Instance instance = e as Instance;
                    tr = instance.GetTotalTransform();
                }

                IList <Autodesk.Revit.DB.Solid> temp = new List <Autodesk.Revit.DB.Solid>();

                foreach (GeometryObject go in e.get_Geometry(new Options()))
                {
                    if (go is GeometryInstance)
                    {
                        GeometryInstance geoInstance = go as GeometryInstance;

                        foreach (var gi in geoInstance.SymbolGeometry)
                        {
                            if (gi is Autodesk.Revit.DB.Solid)
                            {
                                Autodesk.Revit.DB.Solid s = gi as Autodesk.Revit.DB.Solid;
                                s = SolidUtils.CreateTransformed(s, tr);
                                temp.Add(s);
                            }
                        }
                    }
                    else
                    {
                        if (go is Autodesk.Revit.DB.Solid)
                        {
                            Autodesk.Revit.DB.Solid s = go as Autodesk.Revit.DB.Solid;
                            s = SolidUtils.CreateTransformed(s, tr);
                            temp.Add(s);
                        }
                    }
                }

                if (temp.Count > 0)
                {
                    Autodesk.Revit.DB.Solid s0 = temp[0];
                    for (int i = 1; i < temp.Count; ++i)
                    {
                        s0 = BooleanOperationsUtils.ExecuteBooleanOperation(s0, temp[i], BooleanOperationsType.Union);
                    }

                    solids.Add(s0.ToProtoType());
                }
            }

            foreach (RevitLinkInstance rli in new FilteredElementCollector(doc).OfClass(typeof(RevitLinkInstance)).WhereElementIsNotElementType())
            {
                Autodesk.Revit.DB.Document link = rli.GetLinkDocument();

                foreach (Autodesk.Revit.DB.Element e in new FilteredElementCollector(link)
                         .WherePasses(orFilter)
                         .WhereElementIsNotElementType()
                         .Where(x =>
                                x.Parameters.Cast <Autodesk.Revit.DB.Parameter>()
                                .First(p => p.Id.IntegerValue == (int)BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).HasValue))
                {
                    Transform tr = rli.GetTotalTransform();

                    string comments = e.Parameters.Cast <Autodesk.Revit.DB.Parameter>().First(p => p.Id.IntegerValue == (int)BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).AsString();

                    if (comments.ToLower() != "opening")
                    {
                        continue;
                    }

                    if (e is Instance)
                    {
                        Instance instance = e as Instance;
                        tr = tr.Multiply(instance.GetTotalTransform());
                    }

                    IList <Autodesk.Revit.DB.Solid> temp = new List <Autodesk.Revit.DB.Solid>();

                    foreach (var go in e.get_Geometry(new Options()))
                    {
                        if (go is GeometryInstance)
                        {
                            GeometryInstance geoInstance = go as GeometryInstance;

                            foreach (var gi in geoInstance.SymbolGeometry)
                            {
                                if (gi is Autodesk.Revit.DB.Solid)
                                {
                                    Autodesk.Revit.DB.Solid s = gi as Autodesk.Revit.DB.Solid;
                                    s = SolidUtils.CreateTransformed(s, tr);
                                    temp.Add(s);
                                }
                            }
                        }
                        else
                        {
                            if (go is Autodesk.Revit.DB.Solid)
                            {
                                Autodesk.Revit.DB.Solid s = go as Autodesk.Revit.DB.Solid;
                                s = SolidUtils.CreateTransformed(s, tr);
                                temp.Add(s);
                            }
                        }
                    }

                    if (temp.Count > 0)
                    {
                        Autodesk.Revit.DB.Solid s0 = temp[0];
                        for (int i = 1; i < temp.Count; ++i)
                        {
                            s0 = BooleanOperationsUtils.ExecuteBooleanOperation(s0, temp[i], BooleanOperationsType.Union);
                        }

                        solids.Add(s0.ToProtoType());
                    }
                }
            }

            foreach (var s in solids)
            {
                IList <Autodesk.DesignScript.Geometry.Point> points = new List <Autodesk.DesignScript.Geometry.Point>();

                foreach (var v in s.Vertices)
                {
                    points.Add(v.PointGeometry);
                }

                points = Autodesk.DesignScript.Geometry.Point.PruneDuplicates(points);

                Autodesk.DesignScript.Geometry.Plane plane = Autodesk.DesignScript.Geometry.Plane.ByBestFitThroughPoints(points);

                plane = Autodesk.DesignScript.Geometry.Plane.ByOriginNormal(points.Last(), plane.Normal);

                IList <Autodesk.DesignScript.Geometry.Point> temp = new List <Autodesk.DesignScript.Geometry.Point>();

                foreach (var p in points)
                {
                    foreach (var q in p.Project(plane, plane.Normal))
                    {
                        temp.Add(q as Autodesk.DesignScript.Geometry.Point);
                    }

                    foreach (var q in p.Project(plane, plane.Normal.Reverse()))
                    {
                        temp.Add(q as Autodesk.DesignScript.Geometry.Point);
                    }
                }

                temp = Autodesk.DesignScript.Geometry.Point.PruneDuplicates(temp);

                CoordinateSystem cs = CoordinateSystem.ByPlane(plane);

                IList <Autodesk.DesignScript.Geometry.Point> relative = new List <Autodesk.DesignScript.Geometry.Point>();

                foreach (var p in temp)
                {
                    relative.Add(p.Transform(cs.Inverse()) as Autodesk.DesignScript.Geometry.Point);
                }

                var min = Autodesk.DesignScript.Geometry.Point.ByCoordinates(relative.Min(p => p.X),
                                                                             relative.Min(p => p.Y),
                                                                             relative.Min(p => p.Z));

                var max = Autodesk.DesignScript.Geometry.Point.ByCoordinates(relative.Max(p => p.X),
                                                                             relative.Max(p => p.Y),
                                                                             relative.Max(p => p.Z));

                double width  = max.X - min.X;
                double height = max.Y - min.Y;

                min = min.Transform(cs) as Autodesk.DesignScript.Geometry.Point;
                max = max.Transform(cs) as Autodesk.DesignScript.Geometry.Point;

                plane = Autodesk.DesignScript.Geometry.Plane.ByOriginNormal(Autodesk.DesignScript.Geometry.Line.ByStartPointEndPoint(min, max).PointAtParameter(0.5), plane.Normal);

                Autodesk.DesignScript.Geometry.Rectangle rectangle = Autodesk.DesignScript.Geometry.Rectangle.ByWidthLength(plane,
                                                                                                                            width,
                                                                                                                            height);

                output.Add(rectangle);

                plane.Dispose();
                cs.Dispose();
                min.Dispose();
                max.Dispose();
            }

            Utils.Log(string.Format("OpeningUtils.GetRectangularOpenings completed.", ""));

            return(output);
        }
Beispiel #13
0
        private Solid orientNode(OrientationStrategy strategy, List <Strut> struts)
        {
            switch (strategy)
            {
            case OrientationStrategy.AllNodesOrientedToWorldXYZ:

                var plane = Plane.ByOriginNormal(Center, Vector.ZAxis());
                var newCs = CoordinateSystem.ByPlane(plane);

                var output = originalGeometry.Transform(newCs) as Solid;
                plane.Dispose();
                newCs.Dispose();

                return(output);

                break;

            case OrientationStrategy.AllNodesSameAsBaseGeo:

                var orgCs = originalGeometry.ContextCoordinateSystem;
                plane = Plane.ByOriginNormal(Center, orgCs.ZAxis);
                newCs = CoordinateSystem.ByPlane(plane);

                output = originalGeometry.Transform(newCs) as Solid;
                plane.Dispose();
                newCs.Dispose();
                orgCs.Dispose();

                return(output);

                break;

            case OrientationStrategy.AverageStrutsVector:

                //orient the cube based on the average normal of the incoming struts
                var spoints     = struts.Select(x => x.LineRepresentation.StartPoint).ToList();
                var epoints     = struts.Select(x => x.LineRepresentation.EndPoint).ToList();
                var vectors     = spoints.Zip(epoints, (x, y) => Vector.ByTwoPoints(x, y)).ToList();
                var averageNorm = averageVector(vectors);
                var revd        = averageNorm.Reverse();
                if (revd.IsAlmostEqualTo(Vector.ByCoordinates(0, 0, 0)))
                {
                    revd = Vector.ByCoordinates(0, 0, 1);
                }
                //reverse the normal so the top face is correct
                plane = Plane.ByOriginNormalXAxis(Center, revd, Vector.ByCoordinates(0, 0, 1));
                newCs = CoordinateSystem.ByPlane(plane);

                output = originalGeometry.Transform(newCs) as Solid;
                plane.Dispose();
                newCs.Dispose();
                spoints.ForEach(x => x.Dispose());
                epoints.ForEach(x => x.Dispose());
                vectors.ForEach(x => x.Dispose());
                averageNorm.Dispose();
                revd.Dispose();


                return(output);

                break;

            case OrientationStrategy.OrientationProvided:

                break;

            default:
                return(NodeGeometry);

                break;
            }
            //eh?
            return(NodeGeometry);
        }