/// <summary> /// Normalizes all points in the selected subregion to the average elevation of the host surface. /// </summary> /// <param name="uiDoc">The document.</param> private void NormalizeSubregionAndPoints(UIDocument uiDoc) { Document doc = uiDoc.Document; // Pick subregion TopographySurface subregion = SiteUIUtils.PickSubregion(uiDoc); TopographySurface toposurface = SiteEditingUtils.GetTopographySurfaceHost(subregion); IList <XYZ> points = SiteEditingUtils.GetPointsFromSubregionExact(subregion); // Get elevation of all points on the toposurface IList <XYZ> allPoints = toposurface.GetPoints(); double elevation = SiteEditingUtils.GetAverageElevation(allPoints); // Edit scope for all changes using (TopographyEditScope editScope = new TopographyEditScope(doc, "Edit TS")) { editScope.Start(toposurface.Id); using (Transaction t = new Transaction(doc, "Normalize terrain")) { t.Start(); // Change all points to same elevation toposurface.ChangePointsElevation(points, elevation); t.Commit(); } editScope.Commit(new TopographyEditFailuresPreprocessor()); } }
/// <summary> /// Prompts the user to select a point, and returns a point near to the associated TopographySurface. /// </summary> /// <param name="uiDoc">The document.</param> /// <param name="toposurface">The target topography surface.</param> /// <param name="message">The selection message.</param> /// <returns>The point.</returns> public static XYZ PickPointNearToposurface(UIDocument uiDoc, TopographySurface toposurface, String message) { // Pick the point XYZ point = uiDoc.Selection.PickPoint(message); // Get the average elevation for the host topography surface double elevation = SiteEditingUtils.GetAverageElevation(toposurface.GetPoints()); // Project the point onto the Z = average elevation plane XYZ viewDirection = uiDoc.ActiveView.ViewDirection.Normalize(); double elevationDelta = (elevation - point.Z) / viewDirection.Z; point = point + viewDirection * elevationDelta; return(point); }
/// <summary> /// Gets all non-boundary points from a TopographySurface or Subregion. /// </summary> /// <param name="toposurface">The TopographySurface or Subregion.</param> /// <returns>The non-boundary points.</returns> public static IList <XYZ> GetNonBoundaryPoints(TopographySurface toposurface) { IList <XYZ> existingPoints = toposurface.GetPoints(); // Remove boundary points from the list. IList <XYZ> boundaryPoints = toposurface.GetBoundaryPoints(); foreach (XYZ boundaryPoint in boundaryPoints) { foreach (XYZ existingPoint in existingPoints) { if (boundaryPoint.IsAlmostEqualTo(existingPoint, 1e-05)) { existingPoints.Remove(existingPoint); break; } } } return(existingPoints); }
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) { const string _data_folder = "C:/a/vs/DataToBim2/DataToBim/TextForPartialMap/"; try { Document doc = commandData.Application.ActiveUIDocument.Document; #region Save the file ////Edited by Mohammad //SaveFileDialog saveFileDialog = new SaveFileDialog(); //saveFileDialog.Title = "To avoid loosing the data, we recommend saving the file!"; //saveFileDialog.Filter = "Revit Project Files | *.rvt"; //saveFileDialog.DefaultExt = "rvt"; //SaveAsOptions opt = new SaveAsOptions(); //opt.OverwriteExistingFile = true; //bool savefile = true; string fileName = ""; //if (saveFileDialog.ShowDialog() == DialogResult.OK) //{ // fileName = saveFileDialog.FileName; //} //else //{ // savefile = false; //} //if (savefile) //{ // doc.SaveAs(fileName, opt); //} #endregion ///****************************************Change the Paths List <Building> buildings = EnvironmentalComponents.LoadBuildings(_data_folder + "Buildings.txt"); List <Road> roads = EnvironmentalComponents.LoadRoads(_data_folder + "Roads.txt"); List <Contour> allContours = EnvironmentalComponents.LoadContours(_data_folder + "Contour.txt"); #region timer set up Stopwatch timer = Stopwatch.StartNew(); timer.Reset(); StringBuilder report = new StringBuilder(); #endregion #region Getting Topography /* */ timer.Start(); List <List <XYZ> > contours = new List <List <XYZ> >(); foreach (Contour cntr in allContours) { contours.Add(cntr.vertices); } DataToTopography getTopo = new DataToTopography(doc, contours, 10, 5); TopographySurface topoSurface = getTopo.Topography; timer.Stop(); report.AppendLine("Topography Information:"); report.AppendLine(timer.Elapsed.TotalSeconds.ToString() + " seconds took to process the contour lines and get the points."); report.AppendLine(topoSurface.GetPoints().Count.ToString() + " points exist in the topography."); report.AppendLine(getTopo.NumberOfFailedPoints.ToString() + " points were located on the top of each other!"); #endregion #region Save the file //Modified by Mohammad //if (savefile) //{ // doc.SaveAs(fileName, opt); //} #endregion #region Getting Zone-based subregions //timer.Reset(); //timer.Start(); //List<ProcessPolygon> roadsPoints = new List<ProcessPolygon>(); //foreach (Road rd in roads) //{ // roadsPoints.Add(new ProcessPolygon(rd.vertices)); //} //DataToSiteSubRegion getSubregions = new DataToSiteSubRegion(doc, roadsPoints, 300, topoSurface.Id, fileName); //getSubregions.AssignColor(new Color(230, 190, 138)); //timer.Stop(); //report.AppendLine(""); //report.AppendLine("Site subregion geration information:"); //report.AppendLine(timer.Elapsed.TotalSeconds.ToString() + " seconds was needed to create the reagions!"); //report.AppendLine(getSubregions.FailedAttempts.ToString() + " times failed to create subregions in zones"); //#endregion //#region Save the file //if (savefile) //{ // doc.SaveAs(fileName, opt); //} #endregion #region getting the buildings timer.Reset(); timer.Start(); DataToBuilding getBldgs = new DataToBuilding(doc, buildings, getTopo.Topography.Id, fileName); timer.Stop(); report.AppendLine(""); report.AppendLine("Building & building pad geration information:"); report.AppendLine(timer.Elapsed.TotalSeconds.ToString() + " seconds was needed to create the building!"); report.AppendLine(getBldgs.FailedAttemptsToCreateBuildings.ToString() + " times failed to generate buildings!"); report.AppendLine(getBldgs.FailedAttemptsToCreateBuildingPads.ToString() + " times failed to generate building pads!"); #endregion #region Save the file //Modified by Mohammad //if (savefile) //{ // doc.SaveAs(fileName, opt); //} #endregion #region Creating a report TaskDialog.Show("Report of process", report.ToString()); #endregion } catch (Exception exception) { message = exception.Message; return(Autodesk.Revit.UI.Result.Failed); } return(Autodesk.Revit.UI.Result.Succeeded); }
/// <summary> /// Gets the points stored by a subregion. /// </summary> /// <param name="subregion">The subregion.</param> /// <returns>The points</returns> public static IList <XYZ> GetPointsFromSubregionExact(TopographySurface subregion) { return(subregion.GetPoints()); }
/// <summary> /// Moves a subregion and the associated topography to a new user-selected location. /// </summary> /// <param name="uiDoc">The document.</param> private void MoveSubregionAndPoints(UIDocument uiDoc) { Document doc = uiDoc.Document; // Pick subregion TopographySurface subregion = SiteUIUtils.PickSubregion(uiDoc); TopographySurface toposurface = SiteEditingUtils.GetTopographySurfaceHost(subregion); IList <XYZ> points = SiteEditingUtils.GetPointsFromSubregionExact(subregion); XYZ sourceLocation = SiteEditingUtils.GetCenterOf(subregion); // Pick target location XYZ targetPoint = SiteUIUtils.PickPointNearToposurface(uiDoc, toposurface, "Pick point to move to"); // Delta for the move XYZ delta = targetPoint - sourceLocation; // All changes are added to one transaction group - will create one undo item using (TransactionGroup moveGroup = new TransactionGroup(doc, "Move subregion and points")) { moveGroup.Start(); // Get elevation of region in current location IList <XYZ> existingPointsInCurrentLocation = subregion.GetPoints(); double existingElevation = SiteEditingUtils.GetAverageElevation(existingPointsInCurrentLocation); // Move subregion first - allows the command delete existing points and adjust elevation to surroundings using (Transaction t2 = new Transaction(doc, "Move subregion")) { t2.Start(); ElementTransformUtils.MoveElement(doc, subregion.Id, delta); t2.Commit(); } // The boundary points for the subregion cannot be deleted, since they are generated // to represent the subregion boundary rather than representing real points in the host. // Get non-boundary points only to be deleted. IList <XYZ> existingPointsInNewLocation = SiteEditingUtils.GetNonBoundaryPoints(subregion); // Average elevation of all points in the subregion. double newElevation = SiteEditingUtils.GetAverageElevation(subregion.GetPoints()); // Adjust delta for elevation based on calculated values delta = SiteEditingUtils.MoveXYZToElevation(delta, newElevation - existingElevation); // Edit scope for points changes using (TopographyEditScope editScope = new TopographyEditScope(doc, "Edit TS")) { editScope.Start(toposurface.Id); using (Transaction t = new Transaction(doc, "Move points")) { t.Start(); // Delete existing points from target region if (existingPointsInNewLocation.Count > 0) { toposurface.DeletePoints(existingPointsInNewLocation); } // Move points from source region toposurface.MovePoints(points, delta); t.Commit(); } editScope.Commit(new TopographyEditFailuresPreprocessor()); } moveGroup.Assimilate(); } }
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); } }