报告各位,前段时间看世界杯所以我不想更新
给各位放个小案例,以表歉意,再不开心我也没办法咯!
好了,这是一条正经的分割线!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data;
using Autodesk.Revit;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.ApplicationServices;
using System.Data.SqlClient;
using Autodesk.Revit.Creation;
using Autodesk.Revit.DB.Structure;//之前没有记得复制啊
namespace zpx123
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
public class Class1 : IExternalCommand
{
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
//创建应用程序对象
Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application;
//创建文档对象
Autodesk.Revit.DB.Document doc = commandData.Application.ActiveUIDocument.Document;
//创建应用程序对象
Autodesk.Revit.UI.UIApplication uiapp = commandData.Application;
//创建文档对象
Autodesk.Revit.UI.UIDocument uiDoc = uiapp.ActiveUIDocument;
//开始事务
Transaction ts = new Transaction(doc, "支吊架布置");
ts.Start();
以上这是模板,不知道各位还记得不
下面就是正经的代码咯!
之前几期讲过很多创建实例的文章,我们这次来应用一下,平时是不是做支吊架要手工来点很麻烦呢?现在我们做一个小程序让他自动把支吊架布上吧!
2.选择要布置支吊架的管子
我们先添加一个窗体,粗略设计一下窗体,大概就上面这样吧!
给窗体加上代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace zpx123
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void textBox1_TextChanged(object sender, EventArgs e)
{
string str = textBox1.Text;
}
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
public string Number
{
get { return textBox1.Text; }//获得文本框里返回的值
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
正经的代码咯:
double number = 0;
int number1 = 0;
zpx123.Form1 form = new zpx123.Form1();
form.ShowDialog();
if (form.DialogResult == DialogResult.OK)
{
string str = form.Number;
//为了防止用户乱输入发生错误,我习惯踹一脚
try
{
number1 = Convert.ToInt32(str);
number = number1/304.8;
}
catch
{
MessageBox.Show("请输入正确的数字");
}
}
//通过以上这些就可以获得用户输入的数值了
ElementId id = null;
FilteredElementCollector faa = new FilteredElementCollector(doc);
faa.OfClass(typeof(FamilyInstance));//收集族类型
foreach (FamilyInstance fa in faa)
{
if (fa.GetParameters("族与类型")[0].AsValueString().Contains("支吊架"))
{
id = fa.Symbol.Id;
}
}
FamilySymbol fas = doc.GetElement(id) as FamilySymbol;
Selection ree = uiDoc.Selection;
Reference re = ree.PickObject(ObjectType.Element);
Element elem = doc.GetElement(re);
Pipe pipe = elem as Pipe;
LocationCurve curve = pipe.Location as LocationCurve;
XYZ start = curve.Curve.GetEndPoint(0);//获取管道的起点
XYZ end = curve.Curve.GetEndPoint(1);//获得管道的终点
double a = start.X ;
double b = start.Y ;
double zz = start.Z ;
double c = end.X ;
double d = end.Y ;
//以上是获得管道起点和终点的xyz的值
double xy = start.DistanceTo(end) ;//求距离
int gs = (int)((xy / number));//求要放几个支吊架
double y = (number * (d - b)) / Math.Sqrt((c - a) * (c - a) + (d - b) * (d - b)) ;
double x = (number * (c - a)) / Math.Sqrt((c - a) * (c - a) + (d - b) * (d - b)) ;
//以上为数学公式
List<FamilyInstanceCreationData> listt = new List<FamilyInstanceCreationData>();
//用来存储一系列的管道坐标的集合
for (double j = 0; j < gs; j ++)
{
double A = a + x;
double B = b + y;
listt.Add(new FamilyInstanceCreationData(new XYZ(A, B, zz), fas, StructuralType.NonStructural));//把坐标放到集合里
a = A;
b = B;
}
doc.Create.NewFamilyInstances2(listt);//创建支吊架
ts.Commit();
return Result.Succeeded;
}
}
}
(代码有点长,我试了一下,截屏没办法把代码都截进去,只能分成几个图片了)
效果展示:
点击完成,选择管道。
由于支吊架是一个方向的,所以要想完美的实现还需要将支吊架进行旋转,这个就留给各位聪明的宝宝啦!后面某一期会更新如何进行旋转哦!敬请期待(我也不确定啥时候更新)。。。。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有