Example #1
0
        public Pointcloud PointcloudToSpeckle(PointCloudInstance pointcloud, string units = null)
        {
            var u           = units ?? ModelUnits;
            var boundingBox = pointcloud.get_BoundingBox(null);
            var filter      = PointCloudFilterFactory.CreateMultiPlaneFilter(new List <DB.Plane>()
            {
                DB.Plane.CreateByNormalAndOrigin(XYZ.BasisZ, boundingBox.Min)
            });
            var points = pointcloud.GetPoints(filter, 0.0001, 999999); // max limit is 1 mil but 1000000 throws error

            var _pointcloud = new Pointcloud();

            _pointcloud.points = points.Select(o => PointToSpeckle(o, u)).SelectMany(o => new List <double>()
            {
                o.x, o.y, o.z
            }).ToList();
            _pointcloud.colors = points.Select(o => o.Color).ToList();
            _pointcloud.units  = u;
            _pointcloud.bbox   = BoxToSpeckle(boundingBox, u);

            return(_pointcloud);
        }
Example #2
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document   doc   = uidoc.Document;
            Selection  sel   = uidoc.Selection;

            try
            {
                //normalPoint = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_GenericModel).WhereElementIsElementType().Where(t => t.Name.Contains("DebugPoint")).FirstOrDefault() as FamilySymbol;

                if (uidoc.ActiveView is View3D == false)
                {
                    message = Properties.Messages.BeamsFromColumns_Not3dView;
                    return(Result.Failed);
                }

                PointCloudInstance pcInstance = doc.GetElement(sel.PickObject(ObjectType.Element, new PointCloundSelectionFilter(), Properties.Messages.TopoFromPointCloud_SelectPointCloudInstance)) as PointCloudInstance;

                //Element pcInstance = doc.GetElement(sel.PickObject(ObjectType.Element));

                if (pcInstance == null)
                {
                    message = Properties.Messages.TopoFromPointCloud_InvalidPointCloud;
                    return(Result.Failed);
                }



                View3D         this3dView  = uidoc.ActiveView as View3D;
                BoundingBoxXYZ boundingBox = null;

                //Use CropBox if there is one to use
                if (this3dView.CropBoxActive == true)
                {
                    boundingBox = this3dView.CropBox;
                }
                else
                {
                    boundingBox = pcInstance.get_BoundingBox(uidoc.ActiveView);
                }

                boundingBox.Enabled = true;
                Line l1 = Line.CreateBound(boundingBox.Min, boundingBox.Max);

                double crossAngle = XYZ.BasisX.AngleOnPlaneTo(l1.Direction, XYZ.BasisZ);
                double hypotenuse = boundingBox.Min.PlaneDistanceTo(boundingBox.Max);

                double xDirDistance = hypotenuse * Math.Cos(crossAngle);
                double yDirDistance = hypotenuse * Math.Sin(crossAngle);

                double xIncrAmount = 1;
                double yIncrAmount = 1;

                EstabilishIteractionPoints(xDirDistance, ref xNumberOfDivisions, ref xIncrAmount);
                EstabilishIteractionPoints(yDirDistance, ref yNumberOfDivisions, ref yIncrAmount);

                double axIncrAmount = xIncrAmount;
                double ayIncrAmount = yIncrAmount;

                TopoPointCloudUIAdvanced currentUI = new TopoPointCloudUIAdvanced(
                    Math.Round(Utils.ConvertM.feetToM(xDirDistance), 2), Math.Round(Utils.ConvertM.feetToM(yDirDistance), 2), xNumberOfDivisions,
                    yNumberOfDivisions, Math.Round(Utils.ConvertM.feetToM(xIncrAmount), 2), Math.Round(Utils.ConvertM.feetToM(yIncrAmount), 2),
                    pointMaxQuantity);


                currentUI.ShowDialog();

                if (currentUI.DialogResult == false)
                {
                    return(Result.Cancelled);
                }

                xNumberOfDivisions = currentUI.xNumberOfDivisions;
                yNumberOfDivisions = currentUI.yNumberOfDivisions;
                xIncrAmount        = Utils.ConvertM.mToFeet(currentUI.xIncrAmount);
                yIncrAmount        = Utils.ConvertM.mToFeet(currentUI.yIncrAmount);
                pointMaxQuantity   = currentUI.pointMaxQuantity;

                pointDist = pcInstance.GetTotalTransform().Scale *pointDist;
                IList <XYZ> topographyPointList = new List <XYZ>();

                using (Transaction t = new Transaction(doc, Properties.Messages.TopoFromPointCloud_Transaction))
                {
                    t.Start();
                    for (int i = 0; i < xNumberOfDivisions; i++)
                    {
                        for (int j = 0; j < yNumberOfDivisions; j++)
                        {
                            IList <Plane> xPlanes = CreatePlaneBound(doc, boundingBox.Min.FlattenZ(), i, j, xIncrAmount, yIncrAmount, XYZ.BasisX, XYZ.BasisY);
                            IList <Plane> yPlanes = CreatePlaneBound2(doc, boundingBox.Min.FlattenZ(), j, i, yIncrAmount, xIncrAmount, XYZ.BasisY, XYZ.BasisX);
#if R2016
                            Plane planeZ1 = new Plane(XYZ.BasisZ, new XYZ(0, 0, -9999));
                            Plane planeZ2 = new Plane(-XYZ.BasisZ, new XYZ(0, 0, 9999));
#else
                            Plane planeZ1 = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, new XYZ(0, 0, -9999));
                            Plane planeZ2 = Plane.CreateByNormalAndOrigin(-XYZ.BasisZ, new XYZ(0, 0, 9999));
#endif

                            //doc.Create.NewFamilyInstance(planeZ1.Normal, normalPoint, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                            //doc.Create.NewFamilyInstance(planeZ2.Normal, normalPoint, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);

                            IList <Plane> currentPlanes = xPlanes.Union(yPlanes).ToList();
                            currentPlanes.Add(planeZ1);
                            currentPlanes.Add(planeZ2);

                            PointCloudFilter ptFilter        = PointCloudFilterFactory.CreateMultiPlaneFilter(currentPlanes);
                            PointCollection  pointCollection = pcInstance.GetPoints(ptFilter, pointDist, pointMaxQuantity);

                            IList <XYZ> currentBoxPoints = new List <XYZ>();
                            foreach (CloudPoint currentCloudPoint in pointCollection)
                            {
                                XYZ currentXYZ = new XYZ(currentCloudPoint.X, currentCloudPoint.Y, currentCloudPoint.Z);
                                currentXYZ = pcInstance.GetTotalTransform().OfPoint(currentXYZ);
                                if (!ListContainsPoint(currentBoxPoints, currentXYZ))
                                {
                                    currentBoxPoints.Add(currentXYZ);
                                }
                            }

                            topographyPointList = topographyPointList.Union(currentBoxPoints).ToList();
                        }
                    }

                    if (topographyPointList.Count < 3)
                    {
                        message = Properties.Messages.TopoFromPointCloud_NotEnoughPoints;
                        return(Result.Failed);
                    }

                    TopographySurface topoSurface = TopographySurface.Create(doc, topographyPointList);

                    t.Commit();
                }
            }
            catch (Exception excep)
            {
                ExceptionManager eManager = new ExceptionManager(excep);
                return(Result.Cancelled);
            }

            return(Result.Succeeded);
        }
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // 0.import data
            Autodesk.Revit.DB.Wall wall = null;
            Autodesk.Revit.DB.PointCloudInstance element = null;
            double bufferDistance = 0.30; int numPoints = 50000;

            if (!DA.GetData("Revit Wall", ref wall))
            {
                return;
            }
            if (!DA.GetData("Revit Point Cloud Instance", ref element))
            {
                return;
            }
            if (!DA.GetData("bufferDistance", ref bufferDistance))
            {
                return;
            }
            if (!DA.GetData("numPoints", ref numPoints))
            {
                return;
            }

            // 1.Create selection filter
            var width                     = wall.Width + bufferDistance * 2;
            //var width = wall.get_Parameter(BuiltInParameter.WALL_ATTR_WIDTH_PARAM).AsDouble();
            //double width = UnitUtils.ConvertFromInternalUnits(wall.WallType.Width, DisplayUnitType);
            BoundingBoxXYZ boundingBox    = wall.get_BoundingBox(null);

            // 2. Compute boundary U
            LocationCurve locationCurve_u = wall.Location as LocationCurve;
            XYZ           endPoint0_u     = locationCurve_u.Curve.GetEndPoint(0);
            XYZ           endPoint1_u     = locationCurve_u.Curve.GetEndPoint(1);
            var           ux              = endPoint1_u.X - endPoint0_u.X;
            var           uy = endPoint1_u.Y - endPoint0_u.Y;
            XYZ           uxy = new XYZ(ux, uy, 0);

            // 3. Compute boudary V
            XYZ midpoint = endPoint0_u + ((endPoint1_u - endPoint0_u) / 2);
            //var axis =  Autodesk.Revit.DB.Line.CreateUnbound(midpoint, XYZ.BasisZ);
            //var locationCurve_v=locationCurve_u.Rotate(axis, Math.PI * 0.5);

            var endPoint0_v = locationCurve_u.Curve.Evaluate(locationCurve_u.Curve.Length * 0.5 - width * 0.5 * 10 - bufferDistance * 10, false);
            var endPoint1_v = locationCurve_u.Curve.Evaluate(locationCurve_u.Curve.Length * 0.5 + width * 0.5 * 10 + bufferDistance * 10, false);

            // rotate points 90°
            var endPoint0_v_X = (endPoint0_v.X - midpoint.X) * Math.Cos(Math.PI * 0.5) + (endPoint0_v.Y - midpoint.Y) * Math.Sin(Math.PI * 0.5) + midpoint.X;
            var endPoint0_v_Y = (endPoint0_v.X - midpoint.X) * -Math.Sin(Math.PI * 0.5) + (endPoint0_v.Y - midpoint.Y) * Math.Cos(Math.PI * 0.5) + midpoint.Y;
            var endPoint1_v_X = (endPoint1_v.X - midpoint.X) * Math.Cos(Math.PI * 0.5) + (endPoint1_v.Y - midpoint.Y) * Math.Sin(Math.PI * 0.5) + midpoint.X;
            var endPoint1_v_Y = (endPoint1_v.X - midpoint.X) * -Math.Sin(Math.PI * 0.5) + (endPoint1_v.Y - midpoint.Y) * Math.Cos(Math.PI * 0.5) + midpoint.Y;
            XYZ boundary0_v = new XYZ(endPoint0_v_X, endPoint0_v_Y, 0);
            XYZ boundary1_v = new XYZ(endPoint1_v_X, endPoint1_v_Y, 0);
            //XYZ endPoint0_v = locationCurve_u.Curve.GetEndPoint(0);
            //XYZ endPoint1_v = locationCurve_u.Curve.GetEndPoint(1);
            var vx = boundary1_v.X - boundary0_v.X;
            var vy = boundary1_v.Y - boundary0_v.Y;
            XYZ vxy = new XYZ(vx, vy, 0);



            // 4. Create boundary planes
            List <Autodesk.Revit.DB.Plane> planes = new List <Autodesk.Revit.DB.Plane>();

            // U boundaries
            planes.Add(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(uxy, endPoint0_u));
            planes.Add(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(-uxy, endPoint1_u));

            // V boundaries
            planes.Add(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(vxy, boundary0_v));
            planes.Add(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(-vxy, boundary1_v));

            // Z boundaries
            planes.Add(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(XYZ.BasisZ, boundingBox.Min));
            planes.Add(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(-XYZ.BasisZ, boundingBox.Max));

            // Create filter
            PointCloudFilter filter = PointCloudFilterFactory.CreateMultiPlaneFilter(planes);

            // 5.Fetch point cloud
            PointCollection cloudPoints   = element.GetPoints(filter, bufferDistance, numPoints);
            PointCloud      Rh_pointCloud = new PointCloud();

            // 6.Convert CloudPoints to rhino point cloud


            if (element.HasColor())
            {
                foreach (CloudPoint point in cloudPoints)
                {
                    // Process each point
                    Point3d point3d = new Point3d(point.X * 1000, point.Y * 1000, point.Z * 1000);

                    byte[] bArray = BitConverter.GetBytes(point.Color);
                    var    color  = System.Drawing.Color.FromArgb(bArray[0], bArray[1], bArray[2]);

                    Rh_pointCloud.Add(point3d, color);
                }
            }
            else
            {
                foreach (CloudPoint point in cloudPoints)
                {
                    // Process each point
                    Point3d point3d = new Point3d(point.X * 1000, point.Y * 1000, point.Z * 1000);
                    Rh_pointCloud.Add(point3d);
                }
            }


            // 7.Return Grasshopper Cloud
            GH_Cloud GH_pointCloud = new GH_Cloud(Rh_pointCloud);

            DA.SetData(0, GH_pointCloud);
        }
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // 0.import data
            Autodesk.Revit.DB.PointCloudInstance element = null;
            double averageDistance = 0.01; int numPoints = 999999;

            if (!DA.GetData("Revit Point Cloud Instance", ref element))
            {
                return;
            }
            if (!DA.GetData("averageDistance", ref averageDistance))
            {
                return;
            }
            if (!DA.GetData("numPoints", ref numPoints))
            {
                return;
            }

            // 1.Create selection filter
            BoundingBoxXYZ boundingBox = element.get_BoundingBox(null);
            List <Autodesk.Revit.DB.Plane> planes = new List <Autodesk.Revit.DB.Plane>();

            // X boundaries
            planes.Add(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(XYZ.BasisX, boundingBox.Min));
            planes.Add(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(-XYZ.BasisX, boundingBox.Max));

            // Y boundaries
            planes.Add(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(XYZ.BasisY, boundingBox.Min));
            planes.Add(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(-XYZ.BasisY, boundingBox.Max));

            // Z boundaries
            planes.Add(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(XYZ.BasisZ, boundingBox.Min));
            planes.Add(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(-XYZ.BasisZ, boundingBox.Max));

            // Create filter
            PointCloudFilter filter = PointCloudFilterFactory.CreateMultiPlaneFilter(planes);

            // 2.Fetch point cloud
            PointCollection cloudPoints   = element.GetPoints(filter, averageDistance, numPoints);
            PointCloud      Rh_pointCloud = new PointCloud();

            // 3.Convert CloudPoints to rhino point cloud


            if (element.HasColor())
            {
                foreach (CloudPoint point in cloudPoints)
                {
                    // Process each point
                    Point3d point3d = new Point3d(point.X * 1000, point.Y * 1000, point.Z * 1000);

                    byte[] bArray = BitConverter.GetBytes(point.Color);
                    var    color  = System.Drawing.Color.FromArgb(bArray[0], bArray[1], bArray[2]);

                    Rh_pointCloud.Add(point3d, color);
                }
            }
            else
            {
                foreach (CloudPoint point in cloudPoints)
                {
                    // Process each point
                    Point3d point3d = new Point3d(point.X * 1000, point.Y * 1000, point.Z * 1000);
                    Rh_pointCloud.Add(point3d);
                }
            }


            // 4.Return Grasshopper Cloud
            GH_Cloud GH_pointCloud = new GH_Cloud(Rh_pointCloud);

            DA.SetData(0, GH_pointCloud);
        }
Example #5
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document   doc   = uidoc.Document;

            Selection sel = uidoc.Selection;

            try
            {
                if (uidoc.ActiveView is View3D == false)
                {
                    message = "Você deve estar em uma vista 3d para utilizar este comando. Por favor, entre em uma vista compatível e rode o comando novamente.";
                    return(Result.Failed);
                }

                PointCloudInstance pcInstance = doc.GetElement(sel.PickObject(ObjectType.Element, new PointCloundSelectionFilter(), "Por favor, selecione uma nuvem de pontos")) as PointCloudInstance;

                if (pcInstance == null)
                {
                    message = "Objeto inválido selecionado. Este comando funciona somente com instancias de nuvem de pontos, por favor, rode o comando novamente e selecione uma nuvem de pontos.";
                    return(Result.Failed);
                }

                View3D this3dView = uidoc.ActiveView as View3D;

                BoundingBoxXYZ boundingBox = null;

                //Use CropBox if there is one to use
                if (this3dView.CropBoxActive == true)
                {
                    boundingBox = this3dView.CropBox;
                }
                else
                {
                    boundingBox = pcInstance.get_BoundingBox(uidoc.ActiveView);
                }

                List <Plane> planes   = new List <Plane>();
                XYZ          midpoint = (boundingBox.Min + boundingBox.Max) / 2.0;

#if R2016
                // X boundaries
                planes.Add(new Plane(XYZ.BasisX, boundingBox.Min));
                planes.Add(new Plane(-XYZ.BasisX, boundingBox.Max));

                // Y boundaries
                planes.Add(new Plane(XYZ.BasisY, boundingBox.Min));
                planes.Add(new Plane(-XYZ.BasisY, boundingBox.Max));

                // Z boundaries
                planes.Add(new Plane(XYZ.BasisZ, boundingBox.Min));
                planes.Add(new Plane(-XYZ.BasisZ, boundingBox.Max));
#else
                // X boundaries
                planes.Add(Plane.CreateByNormalAndOrigin(XYZ.BasisX, boundingBox.Min));
                planes.Add(Plane.CreateByNormalAndOrigin(-XYZ.BasisX, boundingBox.Max));

                // Y boundaries
                planes.Add(Plane.CreateByNormalAndOrigin(XYZ.BasisY, boundingBox.Min));
                planes.Add(Plane.CreateByNormalAndOrigin(-XYZ.BasisY, boundingBox.Max));

                // Z boundaries
                planes.Add(Plane.CreateByNormalAndOrigin(XYZ.BasisZ, boundingBox.Min));
                planes.Add(Plane.CreateByNormalAndOrigin(-XYZ.BasisZ, boundingBox.Max));
#endif

                using (Transaction t = new Transaction(doc, "Criar Topografia"))
                {
                    t.Start();
                    // Create filter
                    PointCloudFilter pcFilter = PointCloudFilterFactory.CreateMultiPlaneFilter(planes);
                    pcInstance.FilterAction = SelectionFilterAction.Highlight;

                    PointCollection pcColection = pcInstance.GetPoints(pcFilter, pointDistance, pointMaxQuantity);
                    IList <XYZ>     pcPoints    = new List <XYZ>();
                    XYZ             origin      = pcInstance.GetTotalTransform().Origin;

                    if (pcColection.Count < 3)
                    {
                        t.RollBack();
                        message = "Número de pontos insuficiente para criar a topografia. Por favor utilize outra nuvem de pontos.";
                        return(Result.Failed);
                    }

                    foreach (XYZ currentPoint in pcColection)
                    {
                        XYZ pointCopy = new XYZ(currentPoint.X, currentPoint.Y, currentPoint.Z);
                        pointCopy = pcInstance.GetTotalTransform().OfPoint(pointCopy);

                        if (!ListContainsPoint(pcPoints, pointCopy))
                        {
                            pcPoints.Add(pointCopy);
                        }
                    }

                    TopographySurface topoSurface = TopographySurface.Create(doc, pcPoints);
                    t.Commit();
                }
            }
            catch (Exception excep)
            {
                ExceptionManager eManager = new ExceptionManager(excep);
                return(Result.Cancelled);
            }

            return(Result.Succeeded);
        }