Revit SDK:PointCurveCreation 创建点来拟合曲线

前言

这个例子通过留个例子来展示如何通过点来拟合曲线或者曲面。
Revit SDK:PointCurveCreation 创建点来拟合曲线_第1张图片

内容

PointsParabola

生成抛物线的核心逻辑:

double yctr = 0;
XYZ xyz = null;
ReferencePoint rp = null;
double power = 1.2;
while (power < 1.5){
   double xctr = 0;
   double zctr = 0;
   while (zctr < 100){
      zctr = Math.Pow(xctr, power);
      xyz = new XYZ(xctr, yctr, zctr);
      rp = doc.FamilyCreate.NewReferencePoint(xyz);
      if (xctr > 0){
         xyz = new XYZ(-xctr, yctr, zctr);
         rp = doc.FamilyCreate.NewReferencePoint(xyz);
      }
      xctr++;
   }
   power = power + 0.1;
   yctr = yctr + 50;
   zctr = 0;
}

效果:
Revit SDK:PointCurveCreation 创建点来拟合曲线_第2张图片

PointsOnCurve

生成一条直线,然后在直线上创建点,核心逻辑:

XYZ start = new XYZ(0, 0, 0);
XYZ end = new XYZ(50, 50, 0);
Autodesk.Revit.DB.Line line = Autodesk.Revit.DB.Line.CreateBound(start, end);
Plane geometryPlane = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, start);
SketchPlane skplane = SketchPlane.Create(doc, geometryPlane);
ModelCurve modelcurve = doc.FamilyCreate.NewModelCurve(line, skplane);
for (double i = 0.1; i <= 1; i = i + 0.1){
   PointLocationOnCurve locationOnCurve = new PointLocationOnCurve(PointOnCurveMeasurementType.NormalizedCurveParameter, i, PointOnCurveMeasureFrom.Beginning);
   PointOnEdge poe = app.Create.NewPointOnEdge(modelcurve.GeometryCurve.Reference, locationOnCurve);
   ReferencePoint rp2 = doc.FamilyCreate.NewReferencePoint(poe);
}

效果:
Revit SDK:PointCurveCreation 创建点来拟合曲线_第3张图片

PointsFromExcel

从 Excel 读取点,核心逻辑:

string excelFile = "helix.xlsx";
string filepath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Workbook workbook = excelApp.Workbooks.Open(filepath + "\\" + excelFile,
   Type.Missing, Type.Missing, Type.Missing, Type.Missing,
   Type.Missing, Type.Missing, Type.Missing, Type.Missing,
   Type.Missing, Type.Missing, Type.Missing, Type.Missing,
   Type.Missing, Type.Missing);

Worksheet sheet = (Worksheet)workbook.Sheets[1];
Range excelRange = sheet.UsedRange; object[,] valueArray = (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
for (int i = 1; i <= excelRange.Rows.Count; i++)
{
   XYZ xyz = new XYZ(Convert.ToDouble(valueArray[i, 1]), Convert.ToDouble(valueArray[i, 2]), Convert.ToDouble(valueArray[i, 3]));
   ReferencePoint rp = doc.FamilyCreate.NewReferencePoint(xyz);
}
workbook.Close(false, excelFile, null);

效果:
Revit SDK:PointCurveCreation 创建点来拟合曲线_第4张图片

PointsFromTextFile

从csv文件中读取文本,核心逻辑:

string filename = "sphere.csv";
string filepath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
if (File.Exists(filepath + "\\" + filename)){
   StreamReader readFile = new StreamReader(filepath + "\\" + filename);
   string line;
   while ((line = readFile.ReadLine()) != null){
      string[] data = line.Split(',');
      XYZ xyz = new XYZ(Convert.ToDouble(data[0]), Convert.ToDouble(data[1]), Convert.ToDouble(data[2]));
      ReferencePoint rp = doc.FamilyCreate.NewReferencePoint(xyz);
   }
}

效果:
Revit SDK:PointCurveCreation 创建点来拟合曲线_第5张图片

SineCurve

用点去模拟正弦曲线,核心逻辑:

int pnt_ctr = 0;
double xctr = 0;
XYZ xyz = new XYZ();
ReferencePointArray rparray = new ReferencePointArray();
while (pnt_ctr < 500){
   xyz = new XYZ(xctr, 0, (Math.Cos(xctr)) * 10);
   ReferencePoint rp = doc.FamilyCreate.NewReferencePoint(xyz);
   rparray.Append(rp);
   xctr = xctr + 0.1;
   pnt_ctr++;
}
CurveByPoints curve = doc.FamilyCreate.NewCurveByPoints(rparray);

效果:
Revit SDK:PointCurveCreation 创建点来拟合曲线_第6张图片

CatenaryCurve

用点模拟悬垂线,核心逻辑:

for (double scalingFactor = 1; scalingFactor <= 2; scalingFactor = scalingFactor + 0.5){
   ReferencePointArray rpArray = new ReferencePointArray();
   for (double x = -5; x <= 5; x = x + 0.5)   {
      double y = scalingFactor * Math.Cosh(x / scalingFactor);
      if (y < 50)      {
         ReferencePoint rp = doc.FamilyCreate.NewReferencePoint(new XYZ(x, y, 0));
         rpArray.Append(rp);
      }
   }
   CurveByPoints cbp = doc.FamilyCreate.NewCurveByPoints(rpArray);
}

效果:
Revit SDK:PointCurveCreation 创建点来拟合曲线_第7张图片

CyclicSurface

用点模拟,并生成曲面,核心逻辑:

XYZ xyz = new XYZ();
ReferenceArrayArray refArAr = new ReferenceArrayArray();
int x = 0;
double z = 0;
while (x < 800){
   ReferencePointArray rpAr = new ReferencePointArray();
   int y = 0;
   while (y < 800){
      z = 50 * (Math.Cos((Math.PI / 180) * x) + Math.Cos((Math.PI / 180) * y));
      xyz = new XYZ(x, y, z);
      ReferencePoint rp = doc.FamilyCreate.NewReferencePoint(xyz);
      rpAr.Append(rp);
      y = y + 40;
   }
   CurveByPoints curve = doc.FamilyCreate.NewCurveByPoints(rpAr);
   ReferenceArray refAr = new ReferenceArray();
   refAr.Append(curve.GeometryCurve.Reference);
   refArAr.Append(refAr);
   x = x + 40;
}
Form form = doc.FamilyCreate.NewLoftForm(true, refArAr);

效果:
Revit SDK:PointCurveCreation 创建点来拟合曲线_第8张图片

你可能感兴趣的:(Revit,SDK,介绍,C#)