Beispiel #1
0
        /// <summary>
        /// Gets the points from a rectangular outline surrounding the subregion bounding box.
        /// </summary>
        /// <param name="subregion">The subregion.</param>
        /// <returns>The points.</returns>
        public static IList <XYZ> GetPointsFromSubregionRough(TopographySurface subregion)
        {
            BoundingBoxXYZ bbox = subregion.get_BoundingBox(null);

            // Get toposurface points
            TopographySurface toposurface = GetTopographySurfaceHost(subregion);

            Outline     outline = new Outline(bbox.Min, bbox.Max);
            IList <XYZ> points  = toposurface.FindPoints(outline);

            return(points);
        }
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //Get UIDocument
            UIDocument uidoc = commandData.Application.ActiveUIDocument;

            //Get Document
            Document doc = uidoc.Document;

            //Use active view for generation
            View view = doc.ActiveView;

            try
            {
                //Pick referenceline
                Reference refTopo = uidoc.Selection.PickObject(ObjectType.Element);

                //Retrieve Element
                ElementId         topoId  = refTopo.ElementId;
                Element           topo    = doc.GetElement(topoId);
                TopographySurface surface = topo as TopographySurface;

                BoundingBoxXYZ box = surface.get_BoundingBox(view);

                XYZ max = box.Max;
                XYZ min = box.Min;

                double maxx = max.X;
                double maxy = max.Y;
                double minx = min.X;
                double miny = min.Y;

                XYZ topleft     = new XYZ(minx, maxy, 0);
                XYZ bottomright = new XYZ(maxx, miny, 0);

                double domainx = max.DistanceTo(topleft);       //Math.Abs(maxx - minx);
                double domainy = min.DistanceTo(topleft);       //Math.Abs(maxy - miny);

                int nsquaresA = Convert.ToInt32(Math.Ceiling(domainx * 10 / (GlobVars.density / 30)));
                int nsquaresB = Convert.ToInt32(Math.Ceiling(domainy * 10 / (GlobVars.density / 30)));

                double distA = domainx / nsquaresA;
                double distB = domainy / nsquaresB;

                int[] permutations;

                //Generate noise
                //double permutation to avoid overflow
                if (GlobVars.randomvals == 0)
                {
                    permutations = GlobVars.permutation;
                }
                else
                {
                    var rand = new Random();
                    IEnumerable <int> numbers = Enumerable.Range(0, 256);
                    permutations = numbers.OrderBy(x => rand.Next()).ToArray();
                }


                int[] p = new int[512];

                for (int x = 0; x < 256; x++)
                {
                    p[x]       = permutations[x];
                    p[256 + x] = permutations[x];
                }

                //List<double> zvals = new List<double>();
                double[]   zvals      = new double[(nsquaresA + 1) * (nsquaresB + 1)];
                List <XYZ> topography = new List <XYZ>();

                for (double octave = 1.0; octave < (GlobVars.nOctaves + 1); octave++)
                {
                    for (double yy = 1.0; yy < (nsquaresB + 2); yy++)
                    {
                        for (double xx = 1.0; xx < (nsquaresA + 2); xx++)
                        {
                            double noise = perlin_noise((xx / (GlobVars.speed / (2.0 * octave))), (yy / (GlobVars.speed / (2.0 * octave))), p);
                            int    indx  = (((int)yy - 1) * (nsquaresA + 1) + (int)xx - 1);

                            zvals[indx] += (noise * ((GlobVars.amplitude / 330.0) / (2.0 * octave)));

                            if ((int)octave == GlobVars.nOctaves)
                            {
                                double xval = minx + (distA * ((int)xx - 1));
                                double yval = miny + (distB * ((int)yy - 1));

                                XYZ point = new XYZ(xval, yval, zvals[indx]);

                                topography.Add(point);
                            }
                        }
                    }
                }

                IList <XYZ> toDelete = surface.GetPoints();

                //Place generated lines in Revit
                using (Transaction trans = new Transaction(doc, "Place Tree"))
                {
                    trans.Start();

                    Plane       plane       = Plane.CreateByNormalAndOrigin(min.CrossProduct(max), max);
                    SketchPlane sketchPlane = SketchPlane.Create(doc, plane);

                    TopographySurface       toposurface  = TopographySurface.Create(doc, topography);
                    ICollection <ElementId> deletedIdSet = doc.Delete(topoId);

                    trans.Commit();
                }

                return(Result.Succeeded);
            }
            catch (Exception e)
            {
                message = e.Message;
                return(Result.Failed);
            }
        }