有这么一个需求我需要将其他软件提取管子信息在AutoCAD中重新搭建3D管线模型
数据如下
最终AutoCAD模型如下
疯转的管子信息代码如下
public class PipeData
{
public string Name { get; set; }
public string MaterialGrade { get; set; }
public string CrossSectionName { get; set; }
public Point3d StartPoint { get; set; }
public Point3d EndPoint { get; set; }
public static List<PipeData> ReadDataFromExcel(string excelFn)
{
dynamic wb = Microsoft.VisualBasic.Interaction.GetObject(excelFn);
var ws = wb.Sheets[1];
int lastRow = ws.Range["A65536"].End[-4162].Row;
var dataArray = ws.Range["A2:E" + lastRow].Value2 as object[,];
Marshal.FinalReleaseComObject(ws);
wb.Close(false);
//if (wb!=null) Marshal.FinalReleaseComObject(wb);
List<PipeData> res = new List<PipeData>();
for (var i = 1; i <= dataArray.GetLength(0); i++)
{
var str = new List<string>();
for (var j = 1; j <= dataArray.GetLength(1); j++)
{
str.Add(dataArray[i, j] != null ? dataArray[i, j].ToString() : "");
}
var pd = new PipeData(str);
res.Add(pd);
}
return res;
}
public PipeData(List<string> strs)
{
//P1A DNVGL VL E36 PIPE 610x45 (134311.000,6996.000,29329.000) (122550.000,9077.500,12200.000)
Name = strs.FirstOrDefault();
MaterialGrade = strs[1];
CrossSectionName = strs[2];
var sparr = strs[3].Replace("(", "").Replace(")", "").Split(',').Select(c => double.Parse(c)).ToArray();
StartPoint = new Point3d(sparr);
var eparr = strs[4].Replace("(", "").Replace(")", "").Split(',').Select(c => double.Parse(c)).ToArray();
EndPoint = new Point3d(eparr);
}
public double OuterDiameter
{
get
{
return double.Parse(CrossSectionName.Split('x')[0].Replace("PIPE", "").Trim());
}
}
public double WallThickness
{
get
{
return double.Parse(CrossSectionName.Split('x')[1].Trim());
}
}
public Vector3d AixsZ
{
get
{
return EndPoint.Z > StartPoint.Z ? StartPoint.GetVectorTo(EndPoint) : EndPoint.GetVectorTo(StartPoint);
}
}
public ObjectId DrawPipeSolid(Database db)
{
var cir = new Circle(StartPoint.Z > EndPoint.Z ? EndPoint : StartPoint, AixsZ, OuterDiameter);
var s3d = new Solid3d();
var objcol = new DBObjectCollection();
objcol.Add(cir);
Region reg = Region.CreateFromCurves(objcol).Cast<Region>().FirstOrDefault();
s3d.Extrude(reg, StartPoint.DistanceTo(EndPoint), Math.PI * 0);
cir.Dispose();
return AcEnv.CurDb.AppendEntities(s3d).Cast<ObjectId>().FirstOrDefault();
}
}
业务代码如下
[CommandMethod("myss9")]
public static void 绘制圆柱()
{
try
{
using var doclock = AcEnv.CurDocument.LockDocument();
//read excel table
var fn = "D:\\NetDriveDir\\OneDrive\\Desktop\\Pipe WorkPoint.xlsx";
var res = PipeData.ReadDataFromExcel(fn);
foreach (var item in res)
{
item.DrawPipeSolid(AcEnv.CurDb);
}
}
catch (System.Exception ex)
{
AcApp.Application.ShowAlertDialog(ex.StackTrace);
}
}
标签:圆柱,EndPoint,AutoCAD,get,C#,var,new,StartPoint,public From: https://www.cnblogs.com/NanShengBlogs/p/18214244