private static bool GetRectCorners(dg.PolyCurve crv, out rdb.XYZ corner0, out rdb.XYZ corner1) { if (crv.NumberOfCurves != 4) { corner0 = null; corner1 = null; return(false); } List <rdb.XYZ> points = new List <rdb.XYZ>(); foreach (dg.Curve c in crv.Curves()) { points.Add(c.StartPoint.ToXyz()); } points.Sort((x, y) => x.Z.CompareTo(y.Z)); corner0 = points[0]; var vect2 = points[2] - corner0; if (vect2.AngleTo(rdb.XYZ.BasisZ) > 0.01) { corner1 = points[2]; return(true); } corner1 = points[3]; return(true); }
/// <summary> /// Move a point or list of points along a curve based on a parameter witin a range /// </summary> /// <param name="point"></param> /// <param name="polycurve"></param> /// <param name="length"></param> /// <param name="param"></param> /// <search></search> public static dynamic MoveAlongCurve(this DSGeom.Point point, DSGeom.PolyCurve polycurve, double length, double param) { if (length > polycurve.Length) { return("Select a segment length smaller than the length of the polycurve."); } else { double absLength = DSCore.Math.Abs(length); double ptParam = polycurve.ParameterAtPoint(point); double dist = polycurve.SegmentLengthAtParameter(ptParam); double minusDist = dist - absLength; double maxDist = dist + absLength; if (minusDist < 0) { minusDist = 0; } if (maxDist > polycurve.Length) { maxDist = polycurve.Length; } Autodesk.DesignScript.Geometry.Point minusPt = polycurve.PointAtSegmentLength(minusDist); Autodesk.DesignScript.Geometry.Point maxPt = polycurve.PointAtSegmentLength(maxDist); double number = DSCore.Math.MapTo(0, 1, param, minusDist, maxDist); Autodesk.DesignScript.Geometry.Point newPoint = polycurve.PointAtSegmentLength(number); return(newPoint); } }
public static Autodesk.Revit.DB.CurveLoop ToRevitType(this Autodesk.DesignScript.Geometry.PolyCurve pcrv, bool performHostUnitConversion = true) { if (!pcrv.IsClosed) { throw new Exception("The input PolyCurve must be closed"); } Autodesk.DesignScript.Geometry.Curve[] crvs = null; if (performHostUnitConversion) { pcrv = pcrv.InHostUnits(); crvs = pcrv.Curves(); pcrv.Dispose(); } else { crvs = pcrv.Curves(); } var cl = new CurveLoop(); foreach (Autodesk.DesignScript.Geometry.Curve curve in crvs) { using (var nc = curve.ToNurbsCurve()) { Autodesk.Revit.DB.Curve converted = nc.ToRevitType(false); cl.Append(converted); } } crvs.ForEach(x => x.Dispose()); return(cl); }
public static object SnowDrift_ByEdgeHeight(Dyn.Curve edgeCurve, bool flipDirectioin, double snowLoad_ground_psf, double snowLoad_flatRoof_psf, double roofLength_lower_ft, double roofLength_upper_ft, double roofHeight_delta_ft, double adjustmentFactor_ft = 10.0) { sDynamoUtilities dynutil = new sDynamoUtilities(); double designLen; double designHeight; double designMaxLoad; double designSnowDensity; dynutil.GetSnowDriftInformation(snowLoad_ground_psf, snowLoad_flatRoof_psf, roofHeight_delta_ft, roofLength_lower_ft, roofLength_upper_ft, out designLen, out designHeight, out designMaxLoad, out designSnowDensity); double fac = designLen; if (flipDirectioin) { fac *= -1; } Dyn.PolyCurve edgeCrvEx = Dyn.PolyCurve.ByJoinedCurves(new Dyn.Curve[1] { edgeCurve.ExtendStart(adjustmentFactor_ft).ExtendEnd(adjustmentFactor_ft) }); Dyn.PolyCurve driftCrv_onRoof = Dyn.PolyCurve.ByJoinedCurves(new Dyn.Curve[1] { edgeCrvEx.Offset(fac) }); Dyn.PolyCurve driftCrv_onEdge = Dyn.PolyCurve.ByJoinedCurves(new Dyn.Curve[1] { edgeCrvEx.Translate(Dyn.Vector.ZAxis().Scale(designHeight)) as Dyn.Curve }); List <Dyn.Solid> snows = new List <Dyn.Solid>(); for (int i = 0; i < driftCrv_onRoof.NumberOfCurves; ++i) { List <Dyn.PolyCurve> cs = new List <Dyn.PolyCurve>(); cs.Add(Dyn.PolyCurve.ByPoints(new Dyn.Point[3] { driftCrv_onRoof.CurveAtIndex(i).StartPoint, driftCrv_onEdge.CurveAtIndex(i).StartPoint, edgeCrvEx.CurveAtIndex(i).StartPoint }, true)); cs.Add(Dyn.PolyCurve.ByPoints(new Dyn.Point[3] { driftCrv_onRoof.CurveAtIndex(i).EndPoint, driftCrv_onEdge.CurveAtIndex(i).EndPoint, edgeCrvEx.CurveAtIndex(i).EndPoint }, true)); snows.Add(Dyn.Solid.ByLoft(cs)); } Dyn.Solid snowSolid = Dyn.Solid.ByUnion(snows); // edgeCrvEx.Dispose(); // driftCrv_onEdge.Dispose(); // driftCrv_onRoof.Dispose(); return(new Dictionary <string, object> { { "DesignLength_ft", designLen }, { "DesignHeight_ft", designHeight }, { "DesignMaxLoad_psf", designMaxLoad }, { "DesignDensity_pcf", designSnowDensity }, { "DrfitVolumeSolid", snowSolid }, }); }
public static re.Element WallByProfile(List <dg.PolyCurve> closedProfiles, re.WallType wallType, re.Level level) { rdb.Document doc = DocumentManager.Instance.CurrentDBDocument; // Try to get a wall from trace var wallElem = ElementBinder.GetElementFromTrace <rdb.Wall>(doc); dg.PolyCurve closedProfile = closedProfiles[0]; if (!closedProfile.IsClosed || !closedProfile.IsPlanar) { DeleteWall(wallElem, true); return(null); } // Verify the wall profile is vertical dg.Plane basePlane = closedProfile.BasePlane(); if (Math.Abs(basePlane.Normal.Z) > 0.0001) { DeleteWall(wallElem, true); return(null); } // Convert Polycurve segments to a list of Revit curves; List <rdb.Curve> rCrvs = new List <rdb.Curve>(); foreach (dg.PolyCurve pCrv in closedProfiles) { List <dg.Curve> dCrvs = pCrv.Curves().ToList(); foreach (dg.Curve dCrv in dCrvs) { rdb.Curve rCrv = dCrv.ToRevitType(); rCrvs.Add(rCrv); } } TransactionManager.Instance.EnsureInTransaction(doc); DeleteWall(wallElem, false); // Build a wall try { rdb.Wall w = rdb.Wall.Create(doc, rCrvs, new rdb.ElementId(wallType.Id), new rdb.ElementId(level.Id), false); re.Wall rWall = re.ElementWrapper.ToDSType(w, true) as re.Wall; TransactionManager.Instance.TransactionTaskDone(); ElementBinder.CleanupAndSetElementForTrace(doc, w); return(rWall); } catch (Exception ex) { TransactionManager.Instance.TransactionTaskDone(); ElementBinder.CleanupAndSetElementForTrace(doc, null); } return(null); }
/// <summary> /// Converts a Parasite Polyline to a Dynamo Polyline /// </summary> /// <param name="pl"></param> /// <returns></returns> public static Autodesk.DesignScript.Geometry.PolyCurve ToDynamoType(Parasite_Polyline pl) { Autodesk.DesignScript.Geometry.PolyCurve polyCurve = null; if (pl.Closed) { polyCurve = Autodesk.DesignScript.Geometry.PolyCurve.ByPoints(pl.Vertices.Select(a => ToDynamoType(a)).ToArray(), true); } else { polyCurve = Autodesk.DesignScript.Geometry.PolyCurve.ByPoints(pl.Vertices.Select(a => ToDynamoType(a)).ToArray(), false); } return(polyCurve); }
internal List <Dyn.PolyCurve> SplitSegmentizeCurveByCurves(Dyn.Curve c, List <Dyn.Curve> crvs, double intTol, double segTol, out List <Dyn.PolyCurve> segCrvs, out List <Dyn.Point> associatedPts, List <Dyn.Point> pts = null) { List <Dyn.Geometry> disposeGeo = new List <Dyn.Geometry>(); List <Dyn.PolyCurve> pls = new List <Dyn.PolyCurve>(); List <Dyn.PolyCurve> segcrvs = new List <Dyn.PolyCurve>(); List <Dyn.Point> assPts = new List <Dyn.Point>(); foreach (Dyn.Curve spc in SplitCurveByCurves(c, crvs, intTol, out assPts, pts)) { int segCount = (int)(spc.Length / segTol); //////////// //Do not segmentize if spc is straight line.... How???? //How to deal with nurbsCurve? //////////// if (IsLineCurve(spc)) { segCount = 0; } List <Dyn.Point> segPts = new List <Dyn.Point>(); if (segCount < 1) { segPts.Add(spc.StartPoint); segPts.Add(spc.EndPoint); } else { segPts.Add(spc.StartPoint); foreach (Dyn.Point sp in spc.PointsAtEqualSegmentLength(segCount)) { segPts.Add(sp); disposeGeo.Add(sp); } segPts.Add(spc.EndPoint); } segcrvs.Add(Dyn.PolyCurve.ByJoinedCurves(new Dyn.Curve[] { spc })); Dyn.PolyCurve pl = Dyn.PolyCurve.ByPoints(segPts); pls.Add(pl); } segCrvs = segcrvs; associatedPts = assPts; DisposeGeometries(disposeGeo); return(pls); }
internal sPolyLine TosPolyLine(Dyn.PolyCurve dpc) { List <sXYZ> vertice = new List <sXYZ>(); for (int i = 0; i < dpc.NumberOfCurves; ++i) { vertice.Add(TosXYZ(dpc.CurveAtIndex(i).StartPoint)); if (i == dpc.NumberOfCurves - 1) { vertice.Add(TosXYZ(dpc.CurveAtIndex(0).EndPoint)); } } sPolyLine spl = new sPolyLine(vertice, dpc.IsClosed); return(spl); }
private static bool VerifyRect(dg.PolyCurve crv) { if (crv == null) { return(false); } if (!crv.IsClosed) { return(false); } dg.Curve[] crvs = crv.Curves(); if (crv.NumberOfCurves != 4) { return(false); } dg.Vector v0 = dg.Vector.ByTwoPoints(crvs[0].StartPoint, crvs[0].EndPoint); dg.Vector v1 = dg.Vector.ByTwoPoints(crvs[1].StartPoint, crvs[1].EndPoint); dg.Vector v2 = dg.Vector.ByTwoPoints(crvs[2].StartPoint, crvs[2].EndPoint); dg.Vector v3 = dg.Vector.ByTwoPoints(crvs[3].StartPoint, crvs[3].EndPoint); if (Math.Abs(v0.Length - crvs[0].Length) > 0.001 || Math.Abs(v1.Length - crvs[1].Length) > 0.001 || Math.Abs(v2.Length - crvs[2].Length) > 0.001 || Math.Abs(v3.Length - crvs[3].Length) > 0.001) { return(false); } // verify the angles double a0 = v0.Reverse().AngleWithVector(v1); double a1 = v1.Reverse().AngleWithVector(v2); double a2 = v2.Reverse().AngleWithVector(v3); double a3 = v3.Reverse().AngleWithVector(v0); double deg90 = 90; //Math.PI * 0.5; if (Math.Abs(a0 - deg90) > 0.001 || Math.Abs(a1 - deg90) > 0.001 || Math.Abs(a2 - deg90) > 0.001 || Math.Abs(a3 - deg90) > 0.001) { return(false); } return(true); }
public static PdfAnnotation ToPDFPolygon(this Autodesk.DesignScript.Geometry.PolyCurve polycurve, string content, PdfWriter writer) { List <float> points = new List <float>(); foreach (var curve in polycurve.Curves()) { PDFCoords coords = curve.StartPoint.ToPDFCoords(); points.Add(coords.X); points.Add(coords.Y); } iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(0, 0); var app = new PdfContentByte(writer); var anno = PdfAnnotation.CreatePolygonPolyline(writer, rect, content, false, new PdfArray(points.ToArray())); return(anno); }
/// <summary> /// A PolyCurve is not a curve, this is a special extension method to convert to a Revit CurveLoop /// </summary> /// <param name="pcrv"></param> /// <returns></returns> public static Autodesk.Revit.DB.CurveLoop ToRevitType(this Autodesk.DesignScript.Geometry.PolyCurve pcrv) { if (!pcrv.IsClosed) { throw new Exception("The input PolyCurve must be closed"); } var cl = new CurveLoop(); var crvs = pcrv.Curves(); foreach (Autodesk.DesignScript.Geometry.Curve curve in crvs) { Autodesk.Revit.DB.Curve converted = curve.ToNurbsCurve().ToRevitType(); cl.Append(converted); } return(cl); }
/// <summary> /// Construct a printable polyline. /// </summary> /// <param name="PolyCurve">a</param> /// <param name="MaterialAmount">a</param> /// <param name="Speed">a</param> /// <param name="Toolhead">a</param> /// <param name="MixPercentage">a</param> /// <returns name="PrintPolyline">A newly-constructed ZeroTouchEssentials object.</returns> public static V2GPrintPolyline PrintPolyline( Autodesk.DesignScript.Geometry.PolyCurve PolyCurve, double MaterialAmount = 0.033, double Speed = 700.0, int Toolhead = 0, double MixPercentage = 0.0) { int i = 0; V2GPrintPolyline printPolyline = new V2GPrintPolyline(); foreach (Curve c in PolyCurve.Curves()) { if (i == 0) { printPolyline.AddPrintPosition(V2GDesignScriptGeometry.V2GPoint(c.StartPoint)); } printPolyline.AddPrintPosition(V2GDesignScriptGeometry.V2GPoint(c.EndPoint)); } return(printPolyline); }
internal sCurve TosCurve(Dyn.Curve dc) { sCurve sc = null; Dyn.Line dl = dc as Dyn.Line; if (dl != null) { sc = TosLine(dl); sc.curveType = eCurveType.LINE; dl.Dispose(); } Dyn.PolyCurve pc = dc as Dyn.PolyCurve; if (pc != null) { if (pc.NumberOfCurves == 1) { //// //what if this segement is nurbsCurve?????? //PolyCurve can be joined nurbsCurve!!!!!!!!! /// Dyn.Line dtl = Dyn.Line.ByStartPointEndPoint(pc.StartPoint, pc.EndPoint); sc = TosLine(dtl); sc.curveType = eCurveType.LINE; dtl.Dispose(); } else { sc = TosPolyLine(pc); sc.curveType = eCurveType.POLYLINE; } pc.Dispose(); } // Dyn.NurbsCurve nc = dc as Dyn.NurbsCurve; // if(nc != null) // { // // } return(sc); }
/// <summary> /// Create an Advance Steel Weld Line By PolyCurve /// </summary> /// <param name="polyCurve"> Input Weld PolyCurve</param> /// <param name="objectsToConnect"> Input Weld Connected Objects</param> /// <param name="connectionType"> Input Weld Type - 0-OnSite or 2-InShop</param> /// <returns name="weldLine"> weldLine</returns> public static WeldLine ByPolyCurve(DynGeometry.PolyCurve polyCurve, IEnumerable <SteelDbObject> objectsToConnect, [DefaultArgument("2;")] int connectionType) { List <string> handlesList = Utils.GetSteelDbObjectsToConnect(objectsToConnect); var temp = polyCurve.Curves(); SteelGeometry.Point3d[] astArr = new SteelGeometry.Point3d[temp.Length + 1]; for (int i = 0; i < temp.Length; i++) { Point3d startPoint = Utils.ToAstPoint(temp[i].StartPoint, true); astArr[i] = startPoint; } Point3d endPoint = Utils.ToAstPoint(temp[temp.Length - 1].EndPoint, true); astArr[temp.Length] = endPoint; return(new WeldLine(astArr, handlesList, connectionType, polyCurve.IsClosed)); }
public static Autodesk.Revit.DB.CurveLoop ToRevitType(this Autodesk.DesignScript.Geometry.PolyCurve pcrv, bool performHostUnitConversion = true) { if (!pcrv.IsClosed) { throw new Exception("The input PolyCurve must be closed"); } pcrv = performHostUnitConversion ? pcrv.InHostUnits() : pcrv; var cl = new CurveLoop(); var crvs = pcrv.Curves(); foreach (Autodesk.DesignScript.Geometry.Curve curve in crvs) { Autodesk.Revit.DB.Curve converted = curve.ToNurbsCurve().ToRevitType(false); cl.Append(converted); } return(cl); }
/// <summary> /// Randomly move a point or list of points along a curve within a given percentage range. /// </summary> /// <param name="point"></param> /// <param name="polycurve"></param> /// <param name="percentage"></param> /// <search></search> public static dynamic RandomlyMoveAlongCurve(this DSGeom.Point point, DSGeom.PolyCurve polycurve, double percentage) { if (percentage > 100 || percentage < 0) { return("Select a percentage number between 0-100."); } else { double param = polycurve.ParameterAtPoint(point); double start = param - (percentage / 100); double end = param + (percentage / 100); if (start < 0) { start = 0; } if (end > 1) { end = 1; } double i; List <double> range = new List <double>(); for (i = start; i <= end; i += 0.01) { range.Add(i); } var random = new Random(); int index = random.Next(range.Count); double item = range[index]; Autodesk.DesignScript.Geometry.Point newPoint = polycurve.PointAtParameter(item); return(newPoint); } }
public static Dictionary <string, Autodesk.DesignScript.Geometry.Curve[]> OffsetPerimeterCurves(this Autodesk.DesignScript.Geometry.Surface surface, double offset) { List <Autodesk.DesignScript.Geometry.Curve> srfPerimCrvs = surface.PerimeterCurves().ToList(); Autodesk.DesignScript.Geometry.PolyCurve plyCrv = Autodesk.DesignScript.Geometry.PolyCurve.ByJoinedCurves(srfPerimCrvs); double inOffset; double outOffset; if (offset < 0) { inOffset = offset; outOffset = -offset; } else { inOffset = -offset; outOffset = offset; } Autodesk.DesignScript.Geometry.Curve[] inPerimCrvs; try { List <Autodesk.DesignScript.Geometry.Curve> inOffsetCrv = new List <Autodesk.DesignScript.Geometry.Curve>() { (plyCrv.Offset(inOffset)) }; Autodesk.DesignScript.Geometry.PolyCurve inOffsetPolyCrv = Autodesk.DesignScript.Geometry.PolyCurve.ByJoinedCurves(inOffsetCrv); List <Autodesk.DesignScript.Geometry.Curve> inOffsetCrvList = inOffsetPolyCrv.Curves().ToList(); List <Autodesk.DesignScript.Geometry.Point> inPts = new List <Autodesk.DesignScript.Geometry.Point>(); foreach (Autodesk.DesignScript.Geometry.Curve c in inOffsetCrvList) { inPts.Add(c.StartPoint); } Autodesk.DesignScript.Geometry.PolyCurve inOffsetPolyCrv2 = PolyCurve.ByPoints(inPts, true); Autodesk.DesignScript.Geometry.Surface inOffsetSrf = Autodesk.DesignScript.Geometry.Surface.ByPatch(inOffsetPolyCrv2); inPerimCrvs = inOffsetSrf.PerimeterCurves(); inOffsetSrf.Dispose(); inOffsetPolyCrv.Dispose(); inOffsetPolyCrv2.Dispose(); } catch (Exception) { inPerimCrvs = null; } Autodesk.DesignScript.Geometry.Curve[] outPerimCrvs; try { List <Autodesk.DesignScript.Geometry.Curve> outOffsetCrv = new List <Autodesk.DesignScript.Geometry.Curve>() { (plyCrv.Offset(outOffset)) }; Autodesk.DesignScript.Geometry.PolyCurve outOffsetPolyCrv = Autodesk.DesignScript.Geometry.PolyCurve.ByJoinedCurves(outOffsetCrv); List <Autodesk.DesignScript.Geometry.Curve> outOffsetCrvList = outOffsetPolyCrv.Curves().ToList(); List <Autodesk.DesignScript.Geometry.Point> outPts = new List <Autodesk.DesignScript.Geometry.Point>(); foreach (Autodesk.DesignScript.Geometry.Curve c in outOffsetCrvList) { outPts.Add(c.StartPoint); } Autodesk.DesignScript.Geometry.PolyCurve outOffsetPolyCrv2 = PolyCurve.ByPoints(outPts, true); Autodesk.DesignScript.Geometry.Surface outOffsetSrf = Autodesk.DesignScript.Geometry.Surface.ByPatch(outOffsetPolyCrv2); outPerimCrvs = outOffsetSrf.PerimeterCurves(); outOffsetSrf.Dispose(); outOffsetPolyCrv.Dispose(); outOffsetPolyCrv2.Dispose(); } catch (Exception) { outPerimCrvs = null; } Dictionary <string, Autodesk.DesignScript.Geometry.Curve[]> newOutput; newOutput = new Dictionary <string, Autodesk.DesignScript.Geometry.Curve[]> { { "insetCrvs", inPerimCrvs }, { "outsetCrvs", outPerimCrvs } }; //Dispose all redundant geometry plyCrv.Dispose(); foreach (Autodesk.DesignScript.Geometry.Curve c in srfPerimCrvs) { c.Dispose(); } return(newOutput); }
Result IExternalCommand.Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Document revitDoc = commandData.Application.ActiveUIDocument.Document; //取得文档 Application revitApp = commandData.Application.Application; //取得应用程序 UIDocument uiDoc = commandData.Application.ActiveUIDocument; //取得当前活动文档 Document document = commandData.Application.ActiveUIDocument.Document; Window1 window1 = new Window1(); //载入族 FamilySymbol familySymbol; using (Transaction tran = new Transaction(uiDoc.Document)) { tran.Start("载入族"); //载入弦杆族 string file = @"C:\Users\zyx\Desktop\2RevitArcBridge\CrossBeam\CrossBeam\source\crossBeam.rfa"; familySymbol = loadFaimly(file, commandData); familySymbol.Activate(); tran.Commit(); } //把这组模型线通过获取首尾点,生成dynamo里的curve List <XYZ> ps = new List <XYZ>(); using (Transaction transaction = new Transaction(uiDoc.Document)) { transaction.Start("选取模型线生成Curve"); Selection sel = uiDoc.Selection; IList <Reference> modelLines = sel.PickObjects(ObjectType.Element, "选一组模型线"); foreach (Reference reference in modelLines) { Element elem = revitDoc.GetElement(reference); ModelLine modelLine = elem as ModelLine; Autodesk.Revit.DB.Curve c = modelLine.GeometryCurve; ps.Add(c.GetEndPoint(0)); ps.Add(c.GetEndPoint(1)); } for (int i = ps.Count - 1; i > 0; i--) { XYZ p1 = ps[i]; XYZ p2 = ps[i - 1]; //注意此处重合点有一个闭合差 if (p1.DistanceTo(p2) < 0.0001) { ps.RemoveAt(i); } } transaction.Commit(); } //做一个revit和dynamo点的转换 DG.CoordinateSystem coordinateSystem = DG.CoordinateSystem.ByOrigin(0, 0, 0);//标准坐标系 List <DG.Point> DGps = new List <DG.Point>(); foreach (XYZ p in ps) { DGps.Add(p.ToPoint(false)); } DG.PolyCurve polyCurve = DG.PolyCurve.ByPoints(DGps); DG.Curve curve = polyCurve as DG.Curve; List <DG.Point> DGCBps = new List <DG.Point>();//横梁的放置点位列表 double StartLength = 0; if (window1.ShowDialog() == true) { //窗口打开并停留,只有点击按键之后,窗口关闭并返回true } //按键会改变window的属性,通过对属性的循环判断来实现对按键的监测 while (!window1.Done) { //选择起点 if (window1.StartPointSelected) { using (Transaction transaction = new Transaction(uiDoc.Document)) { transaction.Start("选择起点"); double r1 = SelectPoint(commandData); DG.Point dgp1 = curve.PointAtParameter(r1); DGCBps.Add(dgp1); StartLength = curve.SegmentLengthAtParameter(r1); transaction.Commit(); } //2、重置window1.StartPointSelected window1.StartPointSelected = false; } if (window1.ShowDialog() == true) { //窗口打开并停留,只有点击按键之后,窗口关闭并返回true } } //在这里根据间距获取到各个点 for (int i = 1; i < window1.cbNumber; i++) { double L = StartLength + window1.cbDistance * i; DG.Point point = curve.PointAtSegmentLength(L); DGCBps.Add(point); MessageBox.Show(i.ToString()); } List <FamilyInstance> instances = new List <FamilyInstance>(); using (Transaction transaction = new Transaction(uiDoc.Document)) { transaction.Start("创建横梁实例"); foreach (DG.Point p in DGCBps) { FamilyInstance familyInstance; familyInstance = CreateFamlyInstance(p, curve, familySymbol, commandData); instances.Add(familyInstance); } transaction.Commit(); } //给每个族实例设置参数 using (Transaction transaction = new Transaction(uiDoc.Document)) { transaction.Start("族实例参数设置"); foreach (FamilyInstance instance in instances) { double h1 = instance.LookupParameter("l1/2").AsDouble(); instance.LookupParameter("l1/2").Set(window1.l1 / 2); instance.LookupParameter("l2").Set(window1.l2); //instance.LookupParameter("h1").Set(window1.l1); //instance.LookupParameter("h2").Set(window1.l1); } transaction.Commit(); } return(Result.Succeeded); }
public static Dictionary <string, object> ByPolyCurve(Revit.Elements.Element cableTrayType, Autodesk.DesignScript.Geometry.PolyCurve polyCurve, double maxLength, Featureline featureline) { Utils.Log(string.Format("CableTray.ByPolyCurve started...", "")); if (!SessionVariables.ParametersCreated) { UtilsObjectsLocation.CheckParameters(DocumentManager.Instance.CurrentDBDocument); } double length = polyCurve.Length; var oType = cableTrayType.InternalElement as Autodesk.Revit.DB.Electrical.CableTrayType; IList <CableTray> output = new List <CableTray>(); IList <Fitting> fittings = new List <Fitting>(); int subdivisions = Convert.ToInt32(Math.Ceiling(length / maxLength)); Utils.Log(string.Format("subdivisions {0}", subdivisions)); IList <Autodesk.DesignScript.Geometry.Point> points = new List <Autodesk.DesignScript.Geometry.Point>(); try { points.Add(polyCurve.StartPoint); foreach (Autodesk.DesignScript.Geometry.Point p in polyCurve.PointsAtEqualChordLength(subdivisions)) { points.Add(p); } points.Add(polyCurve.EndPoint); points = Autodesk.DesignScript.Geometry.Point.PruneDuplicates(points); // This is slow } catch { points = Featureline.PointsByChord(polyCurve, maxLength); // This is slow } Utils.Log(string.Format("Points {0}", points.Count)); var totalTransform = RevitUtils.DocumentTotalTransform(); Autodesk.DesignScript.Geometry.Point s = null; Autodesk.DesignScript.Geometry.Point e = null; Autodesk.DesignScript.Geometry.Point sp = null; Autodesk.DesignScript.Geometry.Point ep = null; Autodesk.DesignScript.Geometry.Curve curve = null; TransactionManager.Instance.EnsureInTransaction(DocumentManager.Instance.CurrentDBDocument); for (int i = 0; i < points.Count - 1; ++i) { s = points[i]; e = points[i + 1]; curve = Autodesk.DesignScript.Geometry.Line.ByStartPointEndPoint(s, e); sp = s.Transform(totalTransform) as Autodesk.DesignScript.Geometry.Point; ep = e.Transform(totalTransform) as Autodesk.DesignScript.Geometry.Point; var pipe = new CableTray(); Autodesk.Revit.DB.MEPCurve fi; if (DocumentManager.Instance.CurrentDBDocument.IsFamilyDocument) { fi = null; } else { fi = Autodesk.Revit.DB.Electrical.CableTray.Create(DocumentManager.Instance.CurrentDBDocument, oType.Id, sp.ToXyz(), ep.ToXyz(), ElementId.InvalidElementId) as Autodesk.Revit.DB.MEPCurve; } pipe.InitObject((Autodesk.Revit.DB.Electrical.CableTray)fi); pipe.SetParameterByName(ADSK_Parameters.Instance.Corridor.Name, featureline.Baseline.CorridorName); pipe.SetParameterByName(ADSK_Parameters.Instance.BaselineIndex.Name, featureline.Baseline.Index); pipe.SetParameterByName(ADSK_Parameters.Instance.Code.Name, featureline.Code); pipe.SetParameterByName(ADSK_Parameters.Instance.Side.Name, featureline.Side.ToString()); pipe.SetParameterByName(ADSK_Parameters.Instance.X.Name, Math.Round(s.X, 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.Y.Name, Math.Round(s.Y, 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.Z.Name, Math.Round(s.Z, 3)); var soe = featureline.GetStationOffsetElevationByPoint(s); pipe.SetParameterByName(ADSK_Parameters.Instance.Station.Name, Math.Round((double)soe["Station"], 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.Offset.Name, Math.Round((double)soe["Offset"], 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.Elevation.Name, Math.Round((double)soe["Elevation"], 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.Update.Name, 1); pipe.SetParameterByName(ADSK_Parameters.Instance.Delete.Name, 0); soe = featureline.GetStationOffsetElevationByPoint(e); pipe.SetParameterByName(ADSK_Parameters.Instance.EndStation.Name, Math.Round((double)soe["Station"], 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.EndOffset.Name, Math.Round((double)soe["Offset"], 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.EndElevation.Name, Math.Round((double)soe["Elevation"], 3)); output.Add(pipe); Utils.Log(string.Format("Pipe {0}", pipe.Id)); } for (int i = 0; i < output.Count - 1; ++i) { Fitting fitting = null; try { fitting = Fitting.Elbow(output[i], output[i + 1]); } catch { } fittings.Add(fitting); } TransactionManager.Instance.TransactionTaskDone(); Utils.Log(string.Format("CableTray.ByPolyCurve completed.", "")); return(new Dictionary <string, object>() { { "CableTray", output }, { "Fittings", fittings } }); }
/// <summary> /// Create a Revit Floor given it's curve outline and Level /// </summary> /// <param name="outline">The outline.</param> /// <param name="floorType">Type of the floor.</param> /// <param name="level">The level.</param> /// <param name="structural">if set to <c>true</c> [structural].</param> /// <returns> /// The floor /// </returns> public static SlopedFloor ByOutlineTypeAndLevel(Autodesk.DesignScript.Geometry.PolyCurve outline, Revit.Elements.FloorType floorType, Revit.Elements.Level level, bool structural) { Utils.Log(string.Format("SlopedFloor.ByOutlineTypeAndLevel started...", "")); try { var profile = new CurveArray(); Autodesk.DesignScript.Geometry.Plane plane = Autodesk.DesignScript.Geometry.Plane.ByBestFitThroughPoints( outline.Curves().Cast <Autodesk.DesignScript.Geometry.Curve>().Select(x => x.StartPoint)); Vector normal = plane.Normal; if (normal.Dot(Vector.ZAxis()) <= 0) { normal = normal.Reverse(); } Autodesk.DesignScript.Geometry.Point origin = plane.Origin; Autodesk.DesignScript.Geometry.Point end = origin.Add(normal); Autodesk.DesignScript.Geometry.Point projection = Autodesk.DesignScript.Geometry.Point.ByCoordinates(end.X, end.Y, -1000); end = Autodesk.DesignScript.Geometry.Point.ByCoordinates(end.X, end.Y, end.Z + 1000); Autodesk.DesignScript.Geometry.Point intersection = null; var result = plane.Intersect(Autodesk.DesignScript.Geometry .Line.ByStartPointEndPoint(end, projection)); if (result.Length > 0) { intersection = result[0] as Autodesk.DesignScript.Geometry.Point; } else { var message = "Couldn't find intersection"; Utils.Log(string.Format("ERROR: SlopedFloor.ByOutlineTypeAndLevel {0}", message)); throw new Exception(message); } Autodesk.DesignScript.Geometry.Curve temp = Autodesk.DesignScript.Geometry.Line.ByBestFitThroughPoints(new Autodesk.DesignScript.Geometry.Point[] { origin, intersection }); PolyCurve flat = PolyCurve.ByJoinedCurves(outline.PullOntoPlane(Autodesk.DesignScript.Geometry.Plane.XY() .Offset(temp.StartPoint.Z)).Explode().Cast <Autodesk.DesignScript.Geometry.Curve>().ToList()); Autodesk.DesignScript.Geometry.Curve flatLine = temp.PullOntoPlane(Autodesk.DesignScript.Geometry.Plane.XY().Offset(temp.StartPoint.Z)); if (Math.Abs(Math.Abs(plane.Normal.Dot(Vector.ZAxis())) - 1) < 0.00001) { var f = Revit.Elements.Floor.ByOutlineTypeAndLevel(flat, floorType, level); f.InternalElement.Parameters.Cast <Autodesk.Revit.DB.Parameter>() .First(x => x.Id.IntegerValue.Equals(Autodesk.Revit.DB.BuiltInParameter.FLOOR_PARAM_IS_STRUCTURAL)) .Set(structural ? 1 : 0); plane.Dispose(); flatLine.Dispose(); flat.Dispose(); origin.Dispose(); end.Dispose(); projection.Dispose(); intersection.Dispose(); temp.Dispose(); return(new SlopedFloor(f.InternalElement as Autodesk.Revit.DB.Floor)); } double slope = (temp.EndPoint.Z - temp.StartPoint.Z) / flatLine.Length; foreach (Autodesk.DesignScript.Geometry.Curve c in flat.Curves()) { profile.Append(c.ToRevitType()); } Autodesk.Revit.DB.Line slopeArrow = flatLine.ToRevitType() as Autodesk.Revit.DB.Line; var ft = floorType.InternalElement as Autodesk.Revit.DB.FloorType; var lvl = level.InternalElement as Autodesk.Revit.DB.Level; var floor = new SlopedFloor(profile, slopeArrow, slope, ft, lvl, structural); floor.Level = level; floor.Floortype = floorType; floor.Structural = structural; //DocumentManager.Regenerate(); plane.Dispose(); flatLine.Dispose(); flat.Dispose(); origin.Dispose(); end.Dispose(); projection.Dispose(); intersection.Dispose(); temp.Dispose(); Utils.Log(string.Format("SlopedFloor.ByOutlineTypeAndLevel completed.", "")); return(floor); } catch (Exception ex) { Utils.Log(string.Format("ERROR: SlopedFloor.ByOutlineTypeAndLevel {0}", ex.Message)); throw ex; } }
public static re.Element CreateWallOpening(re.Wall wall, dg.PolyCurve polyCrv) { try { // Try to get a wall from trace rdb.Document doc = DocumentManager.Instance.CurrentDBDocument; var openingElem = ElementBinder.GetElementFromTrace <rdb.Element>(doc); rdb.Opening opening = null; if (openingElem != null && openingElem.Id.IntegerValue != (int)rdb.BuiltInCategory.OST_SWallRectOpening) { opening = openingElem as rdb.Opening; } if (!VerifyRect(polyCrv)) { if (opening != null) { TransactionManager.Instance.EnsureInTransaction(doc); doc.Delete(opening.Id); TransactionManager.Instance.TransactionTaskDone(); } TaskDialog.Show("Test", "VerifyRect failed"); return(null); } // Find the two corner points. if (!GetRectCorners(polyCrv, out rdb.XYZ corner0, out rdb.XYZ corner1)) { if (opening != null) { TransactionManager.Instance.EnsureInTransaction(doc); doc.Delete(opening.Id); TransactionManager.Instance.TransactionTaskDone(); } TaskDialog.Show("Test", "GetRectCorners failed"); return(null); } if (corner0 == null || corner1 == null) { if (opening != null) { TransactionManager.Instance.EnsureInTransaction(doc); doc.Delete(opening.Id); TransactionManager.Instance.TransactionTaskDone(); } TaskDialog.Show("Test", "one of the corners is null"); return(null); } TransactionManager.Instance.EnsureInTransaction(doc); // Purge the original element if (opening != null) { doc.Delete(opening.Id); } // Build a new opening; try { rdb.Wall w = wall.InternalElement as rdb.Wall; rdb.Opening o = doc.Create.NewOpening(w, corner0, corner1); re.Element rOpening = re.ElementWrapper.ToDSType(o, true) as re.Element; TransactionManager.Instance.TransactionTaskDone(); ElementBinder.CleanupAndSetElementForTrace(doc, o); return(rOpening); } catch (Exception ex) { TransactionManager.Instance.TransactionTaskDone(); TaskDialog.Show("CreateError", ex.ToString()); ElementBinder.CleanupAndSetElementForTrace(doc, null); } } catch (Exception e) { Autodesk.Revit.UI.TaskDialog.Show("Test", "Error:\n" + e.ToString()); throw; } return(null); }
private static Dictionary <string, object> ByPolyCurve(Revit.Elements.Element conduitType, Autodesk.DesignScript.Geometry.PolyCurve polyCurve, double maxLength, Featureline featureline) { Utils.Log(string.Format("Conduit.ByPolyCurve started...", "")); var totalTransform = RevitUtils.DocumentTotalTransform(); var totalTransformInverse = totalTransform.Inverse(); var oType = conduitType.InternalElement as Autodesk.Revit.DB.Electrical.ConduitType; IList <Conduit> output = new List <Conduit>(); IList <Fitting> fittings = new List <Fitting>(); double length = polyCurve.Length; int subdivisions = Convert.ToInt32(Math.Ceiling(length / maxLength)); IList <Autodesk.DesignScript.Geometry.Point> points = new List <Autodesk.DesignScript.Geometry.Point>(); points.Add(polyCurve.StartPoint); foreach (Autodesk.DesignScript.Geometry.Point p in polyCurve.PointsAtEqualChordLength(subdivisions)) { points.Add(p); } points.Add(polyCurve.EndPoint); points = Autodesk.DesignScript.Geometry.Point.PruneDuplicates(points); for (int i = 0; i < points.Count - 1; ++i) { var s = points[i]; var e = points[i + 1]; var curve = Autodesk.DesignScript.Geometry.Line.ByStartPointEndPoint(s, e); UtilsObjectsLocation.CheckParameters(DocumentManager.Instance.CurrentDBDocument); var sp = s.Transform(totalTransform) as Autodesk.DesignScript.Geometry.Point; var ep = e.Transform(totalTransform) as Autodesk.DesignScript.Geometry.Point; var pipe = new Conduit(oType, sp.ToXyz(), ep.ToXyz()); pipe.SetParameterByName(ADSK_Parameters.Instance.Corridor.Name, featureline.Baseline.CorridorName); pipe.SetParameterByName(ADSK_Parameters.Instance.BaselineIndex.Name, featureline.Baseline.Index); pipe.SetParameterByName(ADSK_Parameters.Instance.Code.Name, featureline.Code); pipe.SetParameterByName(ADSK_Parameters.Instance.Side.Name, featureline.Side.ToString()); pipe.SetParameterByName(ADSK_Parameters.Instance.X.Name, Math.Round(s.X, 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.Y.Name, Math.Round(s.Y, 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.Z.Name, Math.Round(s.Z, 3)); var soe = featureline.GetStationOffsetElevationByPoint(s); pipe.SetParameterByName(ADSK_Parameters.Instance.Station.Name, Math.Round((double)soe["Station"], 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.Offset.Name, Math.Round((double)soe["Offset"], 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.Elevation.Name, Math.Round((double)soe["Elevation"], 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.Update.Name, 1); pipe.SetParameterByName(ADSK_Parameters.Instance.Delete.Name, 0); soe = featureline.GetStationOffsetElevationByPoint(e); pipe.SetParameterByName(ADSK_Parameters.Instance.EndStation.Name, Math.Round((double)soe["Station"], 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.EndOffset.Name, Math.Round((double)soe["Offset"], 3)); pipe.SetParameterByName(ADSK_Parameters.Instance.EndElevation.Name, Math.Round((double)soe["Elevation"], 3)); output.Add(pipe); } for (int i = 0; i < output.Count - 1; ++i) { Fitting fitting = null; try { fitting = Fitting.Elbow(output[i], output[i + 1]); } catch { } fittings.Add(fitting); } totalTransform.Dispose(); totalTransformInverse.Dispose(); Utils.Log(string.Format("Conduit.ByPolyCurve completed.", "")); return(new Dictionary <string, object>() { { "Conduit", output }, { "Fittings", fittings } }); }
public void ToPDF(iTextSharp.text.pdf.PdfWriter w) { PdfContentByte cb = w.DirectContent; cb.SetLineWidth((float)Settings.Thickness); if (Settings.Fill != null) { cb.SetColorFill(Settings.Fill.ToPDFColor()); } if (Settings.Stroke != null) { cb.SetColorStroke(Settings.Stroke.ToPDFColor()); } if (Geometry.GetType() == typeof(Dyn.Arc)) { Dyn.Arc arc = Geometry as Dyn.Arc; cb.MoveTo(arc.StartPoint.X, arc.EndPoint.Y); cb.CurveTo(arc.PointAtParameter(0.5).X, arc.PointAtParameter(0.5).Y, arc.EndPoint.X, arc.EndPoint.Y); } else if (Geometry.GetType() == typeof(Dyn.Line)) { Dyn.Line line = Geometry as Dyn.Line; cb.MoveTo(line.StartPoint.X, line.StartPoint.Y); cb.LineTo(line.EndPoint.X, line.EndPoint.Y); } else if (Geometry.GetType() == typeof(Dyn.Circle)) { Dyn.Circle circle = Geometry as Dyn.Circle; cb.Circle(circle.CenterPoint.X, circle.CenterPoint.Y, circle.Radius); } else if (Geometry.GetType() == typeof(Dyn.Ellipse)) { Dyn.Ellipse ellipse = Geometry as Dyn.Ellipse; cb.Ellipse(ellipse.StartPoint.X, ellipse.StartPoint.Y, ellipse.EndPoint.X, ellipse.EndPoint.Y); } else if (Geometry.GetType() == typeof(Dyn.Rectangle)) { Dyn.Rectangle rect = Geometry as Dyn.Rectangle; cb.Rectangle(rect.Center().X, rect.Center().Y, rect.Width, rect.Height); } else if (Geometry.GetType() == typeof(Dyn.Polygon)) { Dyn.Polygon p = Geometry as Dyn.Polygon; foreach (var curve in p.Curves()) { CurveToPDF(curve, cb); } } else if (Geometry.GetType() == typeof(Dyn.PolyCurve)) { Dyn.PolyCurve pc = Geometry as Dyn.PolyCurve; foreach (var curve in pc.Curves()) { CurveToPDF(curve, cb); } } else if (Geometry.GetType() == typeof(Dyn.NurbsCurve)) { Dyn.NurbsCurve nc = Geometry as Dyn.NurbsCurve; foreach (var linearc in nc.ApproximateWithArcAndLineSegments()) { CurveToPDF(linearc, cb); } } else if (Geometry.GetType() == typeof(Dyn.Curve)) { Dyn.Curve curve = Geometry as Dyn.Curve; CurveToPDF(curve, cb); } else { throw new Exception(Properties.Resources.NotSupported); } if (Settings.Fill != null && Settings.Stroke != null) { cb.FillStroke(); } else { if (Settings.Stroke != null) { cb.Stroke(); } if (Settings.Fill != null) { cb.Fill(); } } }