void ReconstructTopographyByPoints ( Document doc, ref Autodesk.Revit.DB.Element element, IList <Rhino.Geometry.Point3d> points, [Optional] IList <Rhino.Geometry.Curve> regions ) { var scaleFactor = 1.0 / Revit.ModelUnits; if (scaleFactor != 1.0) { for (int p = 0; p < points.Count; ++p) { points[p] = points[p] * scaleFactor; } if (regions != null) { foreach (var region in regions) { region.Scale(scaleFactor); } } } //if (element is TopographySurface topography) //{ // using (var editScope = new TopographyEditScope(doc, "TopographyByPoints")) // { // editScope.Start(element.Id); // topography.DeletePoints(topography.GetPoints()); // topography.AddPoints(points.ToHost().ToList()); // foreach (var subRegionId in topography.GetHostedSubRegionIds()) // doc.Delete(subRegionId); // editScope.Commit(new Revit.FailuresPreprocessor()); // } //} //else { ReplaceElement(ref element, TopographySurface.Create(doc, points.ToHost().ToList())); } if (element != null && regions != null && regions.Count > 0) { var curveLoops = regions.Select(region => CurveLoop.Create(region.ToHost().ToList())).ToList(); SiteSubRegion.Create(doc, curveLoops, element.Id); } }
void ReconstructTopographyByPoints ( Document doc, ref Autodesk.Revit.DB.Element element, IList <Rhino.Geometry.Point3d> points, [Optional] IList <Rhino.Geometry.Curve> regions ) { var scaleFactor = 1.0 / Revit.ModelUnits; var xyz = points.Select(x => x.ChangeUnits(scaleFactor).ToHost()).ToArray(); //if (element is TopographySurface topography) //{ // using (var editScope = new TopographyEditScope(doc, "TopographyByPoints")) // { // editScope.Start(element.Id); // topography.DeletePoints(topography.GetPoints()); // topography.AddPoints(points.ToHost().ToList()); // foreach (var subRegionId in topography.GetHostedSubRegionIds()) // doc.Delete(subRegionId); // editScope.Commit(new Revit.FailuresPreprocessor()); // } //} //else { ReplaceElement(ref element, TopographySurface.Create(doc, xyz)); } if (element is object && regions?.Count > 0) { var curveLoops = regions.Select(region => CurveLoop.Create(region.ChangeUnits(scaleFactor).ToHostMultiple().ToArray())).ToArray(); SiteSubRegion.Create(doc, curveLoops, element.Id); } }
/// <summary> /// Adds a new retaining pond. /// </summary> /// <param name="uiDoc">The document.</param> /// <param name="pondRadius">The radius of the pond.</param> private void AddNewRetainingPond(UIDocument uiDoc, double pondRadius) { Document doc = uiDoc.Document; // Find toposurfaces FilteredElementCollector tsCollector = new FilteredElementCollector(doc); tsCollector.OfClass(typeof(TopographySurface)); IEnumerable <TopographySurface> tsEnumerable = tsCollector.Cast <TopographySurface>().Where <TopographySurface>(ts => !ts.IsSiteSubRegion); int count = tsEnumerable.Count <TopographySurface>(); // If there is only on surface, use it. If there is more than one, let the user select the target. TopographySurface targetSurface = null; if (count > 1) // tmp { targetSurface = SiteUIUtils.PickTopographySurface(uiDoc); } else { targetSurface = tsEnumerable.First <TopographySurface>(); } // Pick point and project to plane at toposurface average elevation XYZ point = SiteUIUtils.PickPointNearToposurface(uiDoc, targetSurface, "Pick point for center of pond."); double elevation = point.Z; // Add subregion first, so that any previously existing points can be removed to avoid distorting the new region // Find material "Water" FilteredElementCollector collector = new FilteredElementCollector(doc); collector.OfClass(typeof(Material)); Material mat = collector.Cast <Material>().FirstOrDefault <Material>(m => m.Name == "Water"); // Create subregion curves List <Curve> curves = new List <Curve>(); curves.Add(Arc.Create(point, pondRadius, 0, Math.PI, XYZ.BasisX, XYZ.BasisY)); curves.Add(Arc.Create(point, pondRadius, Math.PI, 2 * Math.PI, XYZ.BasisX, XYZ.BasisY)); CurveLoop curveLoop = CurveLoop.Create(curves); List <CurveLoop> curveLoops = new List <CurveLoop>(); curveLoops.Add(curveLoop); // All changes are added to one transaction group - will create one undo item using (TransactionGroup addGroup = new TransactionGroup(doc, "Add pond group")) { addGroup.Start(); IList <XYZ> existingPoints = null; // Transacton for adding subregion. using (Transaction t2 = new Transaction(doc, "Add subregion")) { t2.Start(); SiteSubRegion region = SiteSubRegion.Create(doc, curveLoops, targetSurface.Id); if (mat != null) { region.TopographySurface.MaterialId = mat.Id; } 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. existingPoints = SiteEditingUtils.GetNonBoundaryPoints(region.TopographySurface); // Average elevation of all points in the subregion to use as base elevation for the pond topography elevation = SiteEditingUtils.GetAverageElevation(region.TopographySurface.GetPoints()); } // Add the topography points to the target surface via edit scope. using (TopographyEditScope editScope = new TopographyEditScope(doc, "Edit TS")) { editScope.Start(targetSurface.Id); // Transaction for points changes using (Transaction t = new Transaction(doc, "Add points")) { t.Start(); // Delete existing points first to avoid conflict if (existingPoints.Count > 0) { targetSurface.DeletePoints(existingPoints); } // Generate list of points to add IList <XYZ> points = SiteEditingUtils.GeneratePondPointsSurrounding(new XYZ(point.X, point.Y, elevation - 3), pondRadius); targetSurface.AddPoints(points); t.Commit(); } editScope.Commit(new TopographyEditFailuresPreprocessor()); } addGroup.Assimilate(); } }
//converting all of the lists into polygons public DataToSiteSubRegion(Document doc, List <ProcessPolygon> Roads, double regionSize, ElementId topographySurfaceId, string fileAddress = "") { this.opt.OverwriteExistingFile = true; this.DocumentFileAddress = fileAddress; _doc = doc; this.TopographySurfaceId = topographySurfaceId; this.Exponent = 5; this.FailedAttempts = 0; #region Contour treatments foreach (ProcessPolygon processedContour in Roads) { processedContour.Flatten(); processedContour.RemoveIdenticalPoints(); processedContour.RemoveClosePoints(.5); processedContour.RemoveCollinearity(4 * Math.PI / 180); //processedContour.ForceToFixList(); } #endregion #region Setting the sizes of regions this.RegionSize = regionSize; double minX = Roads[0].ProcessedPolygon[0].X; double maxX = Roads[0].ProcessedPolygon[0].X; double minY = Roads[0].ProcessedPolygon[0].Y; double maxY = Roads[0].ProcessedPolygon[0].Y; foreach (ProcessPolygon ProcessedPolygon in Roads) { foreach (XYZ item in ProcessedPolygon.ProcessedPolygon) { minX = (minX > item.X) ? item.X : minX; maxX = (maxX < item.X) ? item.X : maxX; minY = (minY > item.Y) ? item.Y : minY; maxY = (maxY < item.Y) ? item.Y : maxY; } } this.XNumber = (int)Math.Ceiling((maxX - minX) / this.RegionSize); this.YNumber = (int)Math.Ceiling((maxY - minY) / this.RegionSize); this.RegionWidth = (maxX - minX) / this.XNumber; this.RegionHeight = (maxY - minY) / this.YNumber; this.ReagionsBoundingBoxes = new BoundingBoxUV[this.XNumber, this.YNumber]; this.CanSiteSubregionBeValid = new bool[this.XNumber, this.YNumber]; for (int i = 0; i < this.XNumber; i++) { for (int j = 0; j < this.YNumber; j++) { double min_u = minX + i * this.RegionWidth; double max_u = min_u + this.RegionWidth; double min_v = minY + j * this.RegionHeight; double max_v = min_v + this.RegionHeight; this.ReagionsBoundingBoxes[i, j] = new BoundingBoxUV(min_u, min_v, max_u, max_v); } } #endregion #region Checking to see if subregions are projectable on the topography this.RayTracerView = this.createView3d(); for (int i = 0; i < this.XNumber; i++) { for (int j = 0; j < this.YNumber; j++) { this.CanSiteSubregionBeValid[i, j] = this.CanSubregionBeValid(this.ReagionsBoundingBoxes[i, j]); } } #endregion #region creating intPoint polygons of roads Polygons initialRoads = new Polygons(); foreach (ProcessPolygon XYZPolygon in Roads) { initialRoads.Add(this.XYZList2Polygon(XYZPolygon.ProcessedPolygon, this.Exponent)); } this.IntRoads = this.XOR(initialRoads); #endregion #region Subtracting buildings' footprints //placeholder: to be completed #endregion #region Creating the site sub-regions for (int i = 0; i < this.XNumber; i++) { for (int j = 0; j < this.YNumber; j++) { if (this.CanSiteSubregionBeValid[i, j]) { bool visualizePolygons = false; Polygons initialPolygons = getPolygonsInRegion(i, j, 10); if (initialPolygons.Count > 0) { CurveLoops curveLoops = this.PolygonsToCurveLoops(initialPolygons); if (curveLoops.Count > 0) { using (Transaction createSubRegions = new Transaction(_doc)) { FailureHandlingOptions failOpt = createSubRegions.GetFailureHandlingOptions(); failOpt.SetFailuresPreprocessor(new WarningSwallower()); createSubRegions.SetFailureHandlingOptions(failOpt); createSubRegions.Start("Create subregions in site"); try { SiteSubRegion siteSubRegion = SiteSubRegion.Create(_doc, curveLoops, this.TopographySurfaceId); this.SiteSubRegions.Add(siteSubRegion); } catch (Exception) { visualizePolygons = true; this.FailedAttempts++; } createSubRegions.Commit(); } if (visualizePolygons) { foreach (Polygon polygon in initialPolygons) { this.Visualize(_doc, polygon, 0.0, this.Exponent); } } if (this.DocumentFileAddress != "") { _doc.SaveAs(this.DocumentFileAddress, this.opt); } } } } } } #endregion }
void CommitInstance ( Document doc, IGH_DataAccess DA, int Iteration, IEnumerable <Rhino.Geometry.Point3d> points, IEnumerable <Rhino.Geometry.Curve> regions ) { var element = PreviousElement(doc, Iteration); if (!element?.Pinned ?? false) { ReplaceElement(doc, DA, Iteration, element); } else { try { var scaleFactor = 1.0 / Revit.ModelUnits; if (scaleFactor != 1.0) { points = points.Select(p => p * scaleFactor); foreach (var region in regions) { region.Scale(scaleFactor); } } TopographySurface topography = null; //if (element is TopographySurface topography) //{ // using (var editScope = new TopographyEditScope(doc, "TopographyByPoints")) // { // editScope.Start(element.Id); // topography.DeletePoints(topography.GetPoints()); // topography.AddPoints(points.ToHost().ToList()); // foreach (var subRegionId in topography.GetHostedSubRegionIds()) // doc.Delete(subRegionId); // editScope.Commit(new Revit.FailuresPreprocessor()); // } //} //else { topography = CopyParametersFrom(TopographySurface.Create(doc, points.ToHost().ToList()), element); element = topography; } if (topography != null && regions.Any()) { var curveLoops = regions.Select(region => CurveLoop.Create(region.ToHost().ToList())).ToList(); SiteSubRegion.Create(doc, curveLoops, topography.Id); } ReplaceElement(doc, DA, Iteration, element); } catch (Exception e) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message); ReplaceElement(doc, DA, Iteration, null); } } }