Revit二次开发——创建墙
创建墙测试
1 using Autodesk.Revit.UI; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using Autodesk.Revit.DB; 8 using System.Windows.Forms; 9 using Autodesk.Revit.Attributes; 10 using MapTools; 11 using System.Runtime.InteropServices; 12 using XBIMApp; 13 using Autodesk.Revit.UI.Selection; 14 using Autodesk.Revit.ApplicationServices; 15 namespace AxBIMTest 16 { 17 [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)] 18 [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)] 19 20 public class AxWallCreate : IExternalCommand 21 { 22 Autodesk.Revit.ApplicationServices.Application app; 23 Autodesk.Revit.DB.Document doc; 24 List<AxWallLine> m_WallPolylines = null; 25 public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) 26 { 27 UIApplication uiApp = commandData.Application; 28 UIDocument uiDoc = uiApp.ActiveUIDocument; 29 app = uiApp.Application; 30 doc = uiDoc.Document; 31 Selection selection = uiDoc.Selection; 32 OpenFileDialog dlg = new OpenFileDialog(); 33 dlg.Title = "打开墙线文件"; 34 dlg.Filter = "(*.shp)|*.shp"; 35 if (dlg.ShowDialog() == DialogResult.OK && dlg.FileName != String.Empty) 36 { 37 String wallFileName = dlg.FileName; 38 MessageBox.Show(wallFileName); 39 ReadWallLinesSHP(wallFileName); 40 String info = "读取线的数目:" + m_WallPolylines.Count; 41 42 MessageBox.Show(info, "信息"); 43 CreateWall(); 44 } 45 46 return Result.Succeeded; 47 } 48 49 //读取墙线文件 50 private void ReadWallLinesSHP(string FILENAME) 51 { 52 IntPtr hShp; 53 hShp = ShapeLib.SHPOpen(FILENAME, "rb+"); 54 m_WallPolylines = new List<AxWallLine>(); 55 // get shape info and verify shapes were created correctly 56 double[] minB = new double[4]; 57 double[] maxB = new double[4]; 58 int nEntities = 0; 59 ShapeLib.ShapeType shapeType = 0; 60 ShapeLib.SHPGetInfo(hShp, ref nEntities, ref shapeType, minB, maxB); 61 double m_MinX = minB[0]; 62 double m_MinY = minB[1]; 63 for (int i = 0; i < nEntities; i++) 64 { 65 int iShape = i; 66 IntPtr pshpObj = ShapeLib.SHPReadObject(hShp, iShape); 67 AxPolyline2d plline = new AxPolyline2d(); 68 ShapeLib.SHPObject shpObj = new ShapeLib.SHPObject(); 69 Marshal.PtrToStructure(pshpObj, shpObj); 70 71 int parts = shpObj.nParts; 72 if (parts > 0) 73 { 74 int[] partStart = new int[parts]; 75 Marshal.Copy(shpObj.paPartStart, partStart, 0, parts); 76 int[] partType = new int[parts]; 77 Marshal.Copy(shpObj.paPartType, partType, 0, parts); 78 79 int number = shpObj.nVertices; 80 double[] m_padfX = new double[number]; 81 Marshal.Copy(shpObj.padfX, m_padfX, 0, number); 82 double[] m_padfY = new double[number]; 83 Marshal.Copy(shpObj.padfY, m_padfY, 0, number); 84 for (int iv = 0; iv < number; iv++) 85 { 86 double x = m_padfX[iv]; 87 double y = m_padfY[iv]; 88 x = x - minB[0]; 89 y = y - minB[1]; 90 Vector2d pt = new Vector2d(x * 1000, y * 1000); 91 plline.polyline.Add(pt);// 92 } 93 AxWallLine wall = new AxWallLine(); 94 wall.WallId = 1000 + i; 95 wall.m_Polyline = plline; 96 wall.m_MaxZ = 3000; 97 wall.m_MinZ = 0; 98 wall.m_Thickness = 150; 99 m_WallPolylines.Add(wall); 100 } 101 ShapeLib.SHPDestroyObject(pshpObj); 102 } 103 ShapeLib.SHPClose(hShp); 104 } 105 106 public void CreateWall() 107 { 108 List<IList<Curve>> m_curves = new List<IList<Curve>>(); ; 109 for (int i = 0; i < m_WallPolylines.Count; i++) 110 { 111 IList<Curve> curves = new List<Curve>(); 112 AxWallLine wall = m_WallPolylines[i]; 113 List<Vector2d> segments = wall.m_Polyline.polyline; 114 for (int j = 0; j < segments.Count - 1; j++) 115 { 116 Vector2d segment0 = segments[j]; 117 Vector2d segment1 = segments[j + 1]; 118 XYZ pt0 = new XYZ(segment0.X, segment0.Y, 0); 119 XYZ pt00 = new XYZ(segment0.X, segment0.Y, 50); 120 XYZ pt1 = new XYZ(segment1.X, segment1.Y, 0); 121 XYZ pt11 = new XYZ(segment1.X, segment1.Y, 50); 122 Line line01 = Line.CreateBound(pt0, pt1); 123 Line line11 = Line.CreateBound(pt1, pt11); 124 Line line10 = Line.CreateBound(pt11, pt00); 125 Line line00 = Line.CreateBound(pt00, pt0); 126 curves.Add(line01); 127 curves.Add(line11); 128 curves.Add(line10); 129 curves.Add(line00); 130 } 131 m_curves.Add(curves); 132 } 133 using (Transaction ts = new Transaction(doc,"AxCreateWall")) 134 { 135 ts.Start(); 136 for (int i = 0; i < m_curves.Count; i++) 137 { 138 IList<Curve> curves_t = m_curves[i]; 139 Wall.Create(doc, curves_t, false); 140 } 141 142 ts.Commit(); 143 } 144 145 } 146 } 147 }
[Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
public class AxWallCreate : IExternalCommand
{
Autodesk.Revit.ApplicationServices.Application app;
Autodesk.Revit.DB.Document doc;
List<AxWallLine> m_WallPolylines = null;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
app = uiApp.Application;
doc = uiDoc.Document;
Selection selection = uiDoc.Selection;
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "打开墙线文件";
dlg.Filter = "(*.shp)|*.shp";
if (dlg.ShowDialog() == DialogResult.OK && dlg.FileName != String.Empty)
{
String wallFileName = dlg.FileName;
MessageBox.Show(wallFileName);
ReadWallLinesSHP(wallFileName);
String info = "读取线的数目:" + m_WallPolylines.Count;
MessageBox.Show(info, "信息");
CreateWall();
}
return Result.Succeeded;
}
//读取墙线文件
private void ReadWallLinesSHP(string FILENAME)
{
IntPtr hShp;
hShp = ShapeLib.SHPOpen(FILENAME, "rb+");
m_WallPolylines = new List<AxWallLine>();
// get shape info and verify shapes were created correctly
double[] minB = new double[4];
double[] maxB = new double[4];
int nEntities = 0;
ShapeLib.ShapeType shapeType = 0;
ShapeLib.SHPGetInfo(hShp, ref nEntities, ref shapeType, minB, maxB);
double m_MinX = minB[0];
double m_MinY = minB[1];
for (int i = 0; i < nEntities; i++)
{
int iShape = i;
IntPtr pshpObj = ShapeLib.SHPReadObject(hShp, iShape);
AxPolyline2d plline = new AxPolyline2d();
ShapeLib.SHPObject shpObj = new ShapeLib.SHPObject();
Marshal.PtrToStructure(pshpObj, shpObj);
int parts = shpObj.nParts;
if (parts > 0)
{
int[] partStart = new int[parts];
Marshal.Copy(shpObj.paPartStart, partStart, 0, parts);
int[] partType = new int[parts];
Marshal.Copy(shpObj.paPartType, partType, 0, parts);
int number = shpObj.nVertices;
double[] m_padfX = new double[number];
Marshal.Copy(shpObj.padfX, m_padfX, 0, number);
double[] m_padfY = new double[number];
Marshal.Copy(shpObj.padfY, m_padfY, 0, number);
for (int iv = 0; iv < number; iv++)
{
double x = m_padfX[iv];
double y = m_padfY[iv];
x = x - minB[0];
y = y - minB[1];
Vector2d pt = new Vector2d(x * 1000, y * 1000);
plline.polyline.Add(pt);//
}
AxWallLine wall = new AxWallLine();
wall.WallId = 1000 + i;
wall.m_Polyline = plline;
wall.m_MaxZ = 3000;
wall.m_MinZ = 0;
wall.m_Thickness = 150;
m_WallPolylines.Add(wall);
}
ShapeLib.SHPDestroyObject(pshpObj);
}
ShapeLib.SHPClose(hShp);
}
public void CreateWall()
{
//List<Curve>;
IList<Curve> curves = new List<Curve>();
Line l1 = Line.CreateBound(XYZ.Zero, new XYZ(150, 0, 0));
Line l2 = Line.CreateBound(XYZ.Zero, new XYZ(50, 0, 50));
Line l3 = Line.CreateBound(new XYZ(50, 0, 50), new XYZ(100, 0, 50));
Line l4 = Line.CreateBound(new XYZ(100, 0, 50), new XYZ(150, 0, 0));
curves.Add(l1);
curves.Add(l2);
curves.Add(l3);
curves.Add(l4);
using (Transaction ts = new Transaction(doc,"AxCreateWall"))
{
ts.Start();
Wall.Create(doc, curves, false);
ts.Commit();
}
for (int i = 0; i < m_WallPolylines.Count; i++)
{
AxWallLine wall = m_WallPolylines[i];
List<Vector2d> segments = wall.m_Polyline.polyline;
for (int j = 0; j < segments.Count-1; j++)
{
Vector2d segment0 = segments[j];
Vector2d segment1 = segments[j+1];
XYZ pt0 = new XYZ(segment0.X, segment0.Y, 0);
XYZ pt1 = new XYZ(segment1.X, segment1.Y, 0);
Line line = Line.CreateBound(pt0, pt1);
//Wall.Create(doc, line, true);
}
}
}
}

作者:太一吾鱼水
文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。
欢迎大家留言交流,转载请注明出处。
浙公网安备 33010602011771号