最近开始翻模旅程,根据跟群友讨论和网上的搜寻。初步方法为先拾取CAD梁的线段,根据线段所在图层,取得所有梁的最长的那2段线。这样情况下,一条梁仅有2段相等的且平行的直线(不在是一个矩形),具体如图1-1所示!。
图 1- 1 测试模型中对于梁的整图
这里要感谢一下CSDN niuge8905版主
https://blog.csdn.net/niuge8905/article/details/77160646 和 JohnnyWu0918
https://me.csdn.net/mye918 从他们的文章中找到了读取CAD线段的一些方法。
接下来上代码。
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Structure;
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class ReadCadCommand : IExternalCommand
{
Application app;
Document doc;
UIDocument uidoc;
///
/// 正常梁宽度
///
const double NormBeamWidth = 1000;//1000mm
///
/// 所有正常梁宽度集合
///
List
WidthList = new List();
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
uidoc = commandData.Application.ActiveUIDocument;
app = commandData.Application.Application;
doc = uidoc.Document;
Reference r;
try
{
r = uidoc.Selection.PickObject(ObjectType.PointOnElement,“选择梁的一边”);//选择一个元素
}
catch(Exception ex)
{
message = "您取消了本次操作!";
return Result.Failed;
}
//Reference r = uidoc.Selection.PickObject(ObjectType.PointOnElement);//选择一个元素
string ss = r.ConvertToStableRepresentation(doc);
Element elem = doc.GetElement(r);
GeometryElement geoElem = elem.get_Geometry(new Options());
GeometryObject geoObj = elem.GetGeometryObjectFromReference(r);
//获取选中的cad图层
Category targetCategory = null;
ElementId graphicsStyleId = null;
if (geoObj.GraphicsStyleId != ElementId.InvalidElementId)
{
graphicsStyleId = geoObj.GraphicsStyleId;
GraphicsStyle gs = doc.GetElement(geoObj.GraphicsStyleId) as GraphicsStyle;
if (gs != null)
{
targetCategory = gs.GraphicsStyleCategory;
var name = gs.GraphicsStyleCategory.Name;
}
}
//隐藏选中的cad图层
Transaction trans = new Transaction(doc, "隐藏图层");
trans.Start();
if (targetCategory != null)
doc.ActiveView.SetCategoryHidden(targetCategory.Id, false);
trans.Commit();
if (geoElem == null || graphicsStyleId == null)
{
message = "几何元素或ID不存在!";
return Result.Failed;
}
List curveArray_List = getCurveArray( doc,geoElem,graphicsStyleId);
List curveArray_List_copy = new List();//复制得到的模型
foreach (var OrginCADModle in curveArray_List)
{
curveArray_List_copy.Add(OrginCADModle);
}
//取得的模型的线的总数量
int LineNumber = curveArray_List.Count();
//存放不匹配的梁的相关线
List NotMatchCadModel = new List();
//存放模型数组的数组
List> CADModelList_List = new List>();
//int i = 0;
//筛选模型
while (curveArray_List.Count() > 0)
{
//存放距离
List distanceList = new List();
//存放对应距离的CADModel
List cADModel_B_List = new List();
var CadModel_A = curveArray_List[0];
curveArray_List.Remove(CadModel_A);//去除取出的梁的二段线段之一
if (curveArray_List.Count() >= 1)
{
foreach (var CadModel_B in curveArray_List)
{
//梁的2个段线非同一长度最大误差为50mm,方向为绝对值(然而sin120°=sin60°)
if ((float)Math.Abs(CadModel_A.rotation) == (float)Math.Abs(CadModel_B.rotation) && Math.Abs(CadModel_A.length - CadModel_B.length)<0.164)
{
double distance = CadModel_A.location.DistanceTo(CadModel_B.location);
distanceList.Add(distance);
cADModel_B_List.Add(CadModel_B);
}
}
if (distanceList.Count() != 0 && cADModel_B_List.Count != 0)
{
double distanceTwoLine = distanceList.Min();
//筛选不正常的宽度,如发现不正常,将CadModel_B继续放入数组
if (distanceTwoLine * 304.8 < NormBeamWidth && distanceTwoLine>0)
{
//TaskDialog.Show("1", (distanceTwoLine * 304.8).ToString());
var CadModel_shortDistance = cADModel_B_List[distanceList.IndexOf(distanceTwoLine)];
curveArray_List.Remove(CadModel_shortDistance);
//1对梁的模型装入数组
List cADModels = new List();
cADModels.Add(CadModel_A);
cADModels.Add(CadModel_shortDistance);
CADModelList_List.Add(cADModels);
//TaskDialog.Show("1", CadModel_A.location.ToString() + "
" + CadModel_shortDistance.location.ToString());
}
}
else
{
NotMatchCadModel.Add(CadModel_A);
}
}
else
{
NotMatchCadModel.Add(CadModel_A);
}
}
TaskDialog.Show("1","未匹配的线有:"+ NotMatchCadModel.Count().ToString()+" 条!
"+"匹配上的有:"+ CADModelList_List.Count().ToString()+" 对!
"+"丢失:"+ (LineNumber- NotMatchCadModel.Count()- CADModelList_List.Count()*2).ToString()+" 条!
");
//梁类别
FamilySymbol BeamTypeName = doc.GetElement(new ElementId(342873)) as FamilySymbol;
//默认标高2
Level level = LevelFilter(doc);
int tranNumber = 0;//用于改变事务的ID
//生成梁
foreach (var cadModelList in CADModelList_List)
{
CADModel cADModel_A = cadModelList[0];
CADModel cADModel_B = cadModelList[1];
//TaskDialog.Show("1", cADModel_A.location.ToString() + "
" + cADModel_B.location.ToString());
var cADModel_A_StratPoint = cADModel_A.curveArray.get_Item(0).GetEndPoint(0);
var cADModel_A_EndPoint = cADModel_A.curveArray.get_Item(0).GetEndPoint(1);
var cADModel_B_StratPoint = cADModel_B.curveArray.get_Item(0).GetEndPoint(0);
var cADModel_B_EndPoint = cADModel_B.curveArray.get_Item(0).GetEndPoint(1);
XYZ ChangeXYZ = new XYZ();
var LineLength = (GetMiddlePoint(cADModel_A_StratPoint, cADModel_B_StratPoint)).DistanceTo(GetMiddlePoint(cADModel_A_EndPoint, cADModel_B_EndPoint));
if (LineLength <0.00328)//梁的2段线起点非同一端。2段线非同一长度,又非同一端的,中间点的误差选择为1mm
{
ChangeXYZ = cADModel_B_StratPoint;
cADModel_B_StratPoint = cADModel_B_EndPoint;
cADModel_B_EndPoint = ChangeXYZ;
}
Curve curve = Line.CreateBound((GetMiddlePoint(cADModel_A_StratPoint, cADModel_B_StratPoint)), GetMiddlePoint(cADModel_A_EndPoint, cADModel_B_EndPoint));
double distance = cADModel_A.location.DistanceTo(cADModel_B.location);
distance = Math.Round(distance * 304.8, 1);//作为梁_b的参数
WidthList.Add(distance);//梁宽度集合
string beamName = "ZBIM矩形梁 " + (float)(distance) + "*" + (float)(600) + "mm";//类型名 宽度*高度
if (!familSymbol_exists(beamName, "ZBIM - 矩形梁", doc))
{
MakeBeamType(beamName, "ZBIM - 矩形梁");
EditBeamType(beamName, (float)(distance), (float)(600));
}
//用于数据显示和选择,已注释
#region
//List columnTypes = new List();
//columnTypes = getBeamTypes(doc);
//bool repeat = false;
//foreach (string context in columnTypes)
//{
// if (context == beamName)
// {
// repeat = true;
// break;
// }
//}
//if (!repeat)
//{
// columnTypes.Add(beamName);
//}
#endregion
using (Transaction transaction = new Transaction(doc))
{
transaction.Start("Beadm Strart Bulid" + tranNumber.ToString());
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_StructuralFraming);
foreach (FamilySymbol beamType in collector)
{
if (beamType.Name == beamName)
{
if (!beamType.IsActive)
{
beamType.Activate();
}
FamilyInstance beamInstance = doc.Create.NewFamilyInstance(curve, beamType, level, StructuralType.Beam);
var Elevation = beamInstance.get_Parameter(BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM);
break;
}
}
transaction.Commit();
}
}
//取得未匹配的和丢失的CADModel
foreach (var cadModelList in CADModelList_List)
{
CADModel cADModel_A = cadModelList[0];
CADModel cADModel_B = cadModelList[1];
curveArray_List_copy.Remove(cADModel_A);
curveArray_List_copy.Remove(cADModel_B);
}
//一个方向的梁
List UpBeamCAdModel = new List();
//一个方向的梁
List CrossBeamCAdModel = new List();
//最大梁宽度
double MaxBeamWidth;
if (WidthList.Count() == 0)
{
MaxBeamWidth = 1000;//1000mm
}
else
{
MaxBeamWidth = WidthList.Max();
}
//判断是否位空
if (curveArray_List_copy.Count() > 1)
{
var OrginRotation = Math.Abs(curveArray_List_copy[0].rotation);
//分流,横在一起,竖在一起
foreach (var cadModle in curveArray_List_copy)
{
if (Math.Abs(cadModle.rotation) == OrginRotation)
{
CrossBeamCAdModel.Add(cadModle);
}
else
{
UpBeamCAdModel.Add(cadModle);
}
}
}
//判单方向的数量
if (CrossBeamCAdModel.Count() > 2)
{
var CrossMinLength = CrossBeamCAdModel.Select(c => c.length).ToList().Min();//取出最小的线段长度
//将大约2倍最小长度的CAd模型编组
var LongCrossLenth = from n in CrossBeamCAdModel
where n.length > CrossMinLength * 2
select n;
//降序
var newA = from n in LongCrossLenth
orderby n.length descending
select n;
List LongCrossLenth_list = newA.ToList();
//存放模型数组的数组A组-第二次
List> CADModelList_Second = new List>();
////查询失败
List> FailCadModel = new List>();
while (LongCrossLenth_list.Count() > 0)
{
//存放距离
// List distanceList = new List();
//存放对应距离的CADModel
List cADModel_B_List = new List();
var CadModel_Main = LongCrossLenth_list[0];//取出主模型
XYZ StartPoint_Main = CadModel_Main.curveArray.get_Item(0).GetEndPoint(0);//取出线段2点
XYZ EndPoint_Main = CadModel_Main.curveArray.get_Item(0).GetEndPoint(1);
LongCrossLenth_list.Remove(CadModel_Main);
CrossBeamCAdModel.Remove(CadModel_Main);
foreach (var cadModelFirst in CrossBeamCAdModel)
{
Line line = CadModel_Main.curveArray.get_Item(0) as Line;//取出主模型的线
if (cadModelFirst.length >= CadModel_Main.length)
{
continue;
}
double PointLineDistance = line.Distance(cadModelFirst.location);
PointLineDistance = Math.Round(PointLineDistance * 304.8, 1);
if (PointLineDistance > MaxBeamWidth || PointLineDistance == 0)
{
continue;
}
XYZ StartPoint_First = cadModelFirst.curveArray.get_Item(0).GetEndPoint(0);//子线段2点
XYZ EndPoint_First = cadModelFirst.curveArray.get_Item(0).GetEndPoint(1);
double A1 = StartPoint_First.DistanceTo(StartPoint_Main);
double A2 = StartPoint_First.DistanceTo(EndPoint_Main);
double B1 = EndPoint_First.DistanceTo(StartPoint_Main);
double B2 = EndPoint_First.DistanceTo(EndPoint_Main);
if (Math.Abs(PointLineDistance - A1 * 304.8) < 1 || Math.Abs(PointLineDistance - A2 * 304.8) < 1 || Math.Abs(PointLineDistance - B1 * 304.8) < 1 || Math.Abs(PointLineDistance - B2 * 304.8) < 1)
{
cADModel_B_List.Add(cadModelFirst);
}
}
List FailedCadmodel = new List();
if (cADModel_B_List.Count() == 1)
{
FailedCadmodel.Add(cADModel_B_List[0]);
FailedCadmodel.Add(CadModel_Main);
FailCadModel.Add(FailedCadmodel);
}
if (cADModel_B_List.Count() == 2)
{
cADModel_B_List.Add(CadModel_Main);
CADModelList_Second.Add(cADModel_B_List);
}
else
{
CrossBeamCAdModel.Add(CadModel_Main);
}
//FailCadModel.Add(FailedCadmodel);
}
////第二组A成功部分生成
foreach (var CAdMidelList in CADModelList_Second)
{
CADModel cADModel_A = CAdMidelList[0];
CADModel cADModel_B = CAdMidelList[1];
CADModel cADModel_Main = CAdMidelList[2];
Line line = cADModel_Main.curveArray.get_Item(0) as Line;
List MinDistance = new List();//最短距离
List MinDistanceB = new List();//最短距离B
XYZ OnePoint = new XYZ();
XYZ TwoPont = new XYZ();
var cADModel_A_StratPoint = cADModel_A.curveArray.get_Item(0).GetEndPoint(0);
var cADModel_A_EndPoint = cADModel_A.curveArray.get_Item(0).GetEndPoint(1);
double distanceA = line.Distance(cADModel_A.location);
var cADModel_B_StratPoint = cADModel_B.curveArray.get_Item(0).GetEndPoint(0);
var cADModel_B_EndPoint = cADModel_B.curveArray.get_Item(0).GetEndPoint(1);
double distanceB = line.Distance(cADModel_B.location);
var cADModel_Main_StratPoint = cADModel_Main.curveArray.get_Item(0).GetEndPoint(0);
var cADModel_Main_EndPoint = cADModel_Main.curveArray.get_Item(0).GetEndPoint(1);
double A1 = cADModel_A_StratPoint.DistanceTo(cADModel_Main_StratPoint);
MinDistance.Add(A1);
double A2 = cADModel_A_StratPoint.DistanceTo(cADModel_Main_EndPoint);
MinDistance.Add(A2);
double A3 = cADModel_A_EndPoint.DistanceTo(cADModel_Main_StratPoint);
MinDistance.Add(A3);
double A4 = cADModel_A_EndPoint.DistanceTo(cADModel_Main_EndPoint);
MinDistance.Add(A4);
double MainDistance_A = MinDistance.Min();
int index_A = MinDistance.IndexOf(MainDistance_A);
switch (index_A)
{
case 0:
OnePoint = cADModel_A_StratPoint;
break;
case 1:
OnePoint = cADModel_A_StratPoint;
break;
case 2:
OnePoint = cADModel_A_EndPoint;
break;
case 3:
OnePoint = cADModel_A_EndPoint;
break;
}
double B1 = cADModel_B_StratPoint.DistanceTo(cADModel_Main_StratPoint);
MinDistanceB.Add(B1);
double B2 = cADModel_B_StratPoint.DistanceTo(cADModel_Main_EndPoint);
MinDistanceB.Add(B2);
double B3 = cADModel_B_EndPoint.DistanceTo(cADModel_Main_StratPoint);
MinDistanceB.Add(B3);
double B4 = cADModel_B_EndPoint.DistanceTo(cADModel_Main_EndPoint);
MinDistanceB.Add(B4);
double MainDistance_b = MinDistanceB.Min();
int index_B = MinDistanceB.IndexOf(MainDistance_b);
switch (index_B)
{
case 0:
TwoPont = cADModel_B_StratPoint;
break;
case 1:
TwoPont = cADModel_B_StratPoint;
break;
case 2:
TwoPont = cADModel_B_EndPoint;
break;
case 3:
TwoPont = cADModel_B_EndPoint;
break;
}
MinDistance.Clear();
MinDistanceB.Clear();
XYZ ChangeXYZ = new XYZ();
var LineLength = (GetMiddlePoint(OnePoint, cADModel_Main_StratPoint)).DistanceTo(GetMiddlePoint(TwoPont, cADModel_Main_EndPoint));
if (LineLength < 0.00328)//梁的2段线起点非同一端。2段线非同一长度,又非同一端的,中间点的误差选择为1mm
{
ChangeXYZ = TwoPont;
TwoPont = OnePoint;
OnePoint = ChangeXYZ;
}
Curve curve = Line.CreateBound((GetMiddlePoint(OnePoint, cADModel_Main_StratPoint)), GetMiddlePoint(TwoPont, cADModel_Main_EndPoint));
double distance = MainDistance_A;//cADModel_A.location.DistanceTo(cADModel_Main.location);
distance = Math.Round(distance * 304.8, 1);//作为梁_b的参数
string beamName = "ZBIM矩形梁 " + (float)(distance) + "*" + (float)(600) + "mm";//类型名 宽度*高度
if (!familSymbol_exists(beamName, "ZBIM - 矩形梁", doc))
{
MakeBeamType(beamName, "ZBIM - 矩形梁");
EditBeamType(beamName, (float)(distance), (float)(600));
}
using (Transaction transaction = new Transaction(doc))
{
transaction.Start("Beadm Strart Bulid" + tranNumber.ToString());
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_StructuralFraming);
foreach (FamilySymbol beamType in collector)
{
if (beamType.Name == beamName)
{
if (!beamType.IsActive)
{
beamType.Activate();
}
FamilyInstance beamInstance = doc.Create.NewFamilyInstance(curve, beamType, level, StructuralType.Beam);
var Elevation = beamInstance.get_Parameter(BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM);
break;
}
}
//foreach()
transaction.Commit();
}
}
//第二组A的失败部分生成
foreach (var CAdMidelList in FailCadModel)
{
CADModel cADModel_A = CAdMidelList[0];
CADModel cADModel_Main = CAdMidelList[1];
Line line = cADModel_Main.curveArray.get_Item(0) as Line;
List MinDistance = new List();
XYZ OnePoint = new XYZ();
XYZ TwoPoint;//用于编造第二点
var cADModel_A_StratPoint = cADModel_A.curveArray.get_Item(0).GetEndPoint(0);
var cADModel_A_EndPoint = cADModel_A.curveArray.get_Item(0).GetEndPoint(1);
var cADModel_Main_StratPoint = cADModel_Main.curveArray.get_Item(0).GetEndPoint(0);
var cADModel_Main_EndPoint = cADModel_Main.curveArray.get_Item(0).GetEndPoint(1);
double A1 = cADModel_A_StratPoint.DistanceTo(cADModel_Main_StratPoint);
MinDistance.Add(A1);
double A2 = cADModel_A_StratPoint.DistanceTo(cADModel_Main_EndPoint);
MinDistance.Add(A2);
double A3 = cADModel_A_EndPoint.DistanceTo(cADModel_Main_StratPoint);
MinDistance.Add(A3);
double A4 = cADModel_A_EndPoint.DistanceTo(cADModel_Main_EndPoint);
MinDistance.Add(A4);
double MainDistance_A = MinDistance.Min();
int index_A = MinDistance.IndexOf(MainDistance_A);
switch (index_A)
{
case 0:
OnePoint = cADModel_A_StratPoint;
break;
case 1:
OnePoint = cADModel_A_StratPoint;
break;
case 2:
OnePoint = cADModel_A_EndPoint;
break;
case 3:
OnePoint = cADModel_A_EndPoint;
break;
}
if ((OnePoint.X - cADModel_Main_StratPoint.X) < 0.001)
{
TwoPoint = new XYZ(cADModel_Main_EndPoint.X, OnePoint.Y, OnePoint.Z);
}
else if ((OnePoint.X - cADModel_Main_EndPoint.X) < 0.001)
{
TwoPoint = new XYZ(cADModel_Main_StratPoint.X, OnePoint.Y, OnePoint.Z);
}
else
{
continue;
}
XYZ ChangeXYZ = new XYZ();
var LineLength = (GetMiddlePoint(OnePoint, cADModel_Main_StratPoint)).DistanceTo(GetMiddlePoint(TwoPoint, cADModel_Main_EndPoint));
if (LineLength < 0.00328)//梁的2段线起点非同一端。2段线非同一长度,又非同一端的,中间点的误差选择为1mm
{
ChangeXYZ = TwoPoint;
TwoPoint = OnePoint;
OnePoint = ChangeXYZ;
}
Curve curve = Line.CreateBound((GetMiddlePoint(OnePoint, cADModel_Main_StratPoint)), GetMiddlePoint(TwoPoint, cADModel_Main_EndPoint));
double distance = MainDistance_A;//cADModel_A.location.DistanceTo(cADModel_Main.location);
distance = Math.Round(distance * 304.8, 1);//作为梁_b的参数
string beamName = "ZBIM矩形梁 " + (float)(distance) + "*" + (float)(600) + "mm";//类型名 宽度*高度
if (!familSymbol_exists(beamName, "ZBIM - 矩形梁", doc))
{
MakeBeamType(beamName, "ZBIM - 矩形梁");
EditBeamType(beamName, (float)(distance), (float)(600));
}
using (Transaction transaction = new Transaction(doc))
{
transaction.Start("Beadm Strart Bulid" + tranNumber.ToString());
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_StructuralFraming);
foreach (FamilySymbol beamType in collector)
{
if (beamType.Name == beamName)
{
if (!beamType.IsActive)
{
beamType.Activate();
}
FamilyInstance beamInstance = doc.Create.NewFamilyInstance(curve, beamType, level, StructuralType.Beam);
var Elevation = beamInstance.get_Parameter(BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM);
break;
}
}
//foreach()
transaction.Commit();
}
}
}
//判断单方向数量
if (UpBeamCAdModel.Count() > 2)
{
var UpMinLength = UpBeamCAdModel.Select(c => c.length).ToList().Min();
var LongUpLength = from n in UpBeamCAdModel
where n.length > UpMinLength * 2
select n;
//降序
var newB = from n in LongUpLength
orderby n.length descending
select n;
List LongUPLenth_list_B = newB.ToList();
//存放模型数组的数组B组-第二次(竖着)
List> CADModelList_Second_B = new List>();
//查询失败B组
List> FailCadModel_B = new List>();
while (LongUPLenth_list_B.Count() > 0)
{
//存放距离
// List distanceList = new List();
//存放对应距离的CADModel
List cADModel_B_List = new List();
var CadModel_Main = LongUPLenth_list_B[0];//取出主模型
XYZ StartPoint_Main = CadModel_Main.curveArray.get_Item(0).GetEndPoint(0);//取出线段2点
XYZ EndPoint_Main = CadModel_Main.curveArray.get_Item(0).GetEndPoint(1);
LongUPLenth_list_B.Remove(CadModel_Main);
UpBeamCAdModel.Remove(CadModel_Main);
foreach (var cadModelFirst in UpBeamCAdModel)
{
Line line = CadModel_Main.curveArray.get_Item(0) as Line;//取出主模型的线
if (cadModelFirst.length >= CadModel_Main.length)
{
continue;
}
double PointLineDistance = line.Distance(cadModelFirst.location);
PointLineDistance = Math.Round(PointLineDistance * 304.8, 1);
if (PointLineDistance > MaxBeamWidth || PointLineDistance == 0)
{
continue;
}
XYZ StartPoint_First = cadModelFirst.curveArray.get_Item(0).GetEndPoint(0);//子线段2点
XYZ EndPoint_First = cadModelFirst.curveArray.get_Item(0).GetEndPoint(1);
double A1 = StartPoint_First.DistanceTo(StartPoint_Main);
double A2 = StartPoint_First.DistanceTo(EndPoint_Main);
double B1 = EndPoint_First.DistanceTo(StartPoint_Main);
double B2 = EndPoint_First.DistanceTo(EndPoint_Main);
if (Math.Abs(PointLineDistance - A1 * 304.8) < 1 || Math.Abs(PointLineDistance - A2 * 304.8) < 1 || Math.Abs(PointLineDistance - B1 * 304.8) < 1 || Math.Abs(PointLineDistance - B2 * 304.8) < 1)
{
cADModel_B_List.Add(cadModelFirst);
}
}
List FailedCadmodel = new List();
if (cADModel_B_List.Count() == 1)
{
FailedCadmodel.Add(cADModel_B_List[0]);
FailedCadmodel.Add(CadModel_Main);
FailCadModel_B.Add(FailedCadmodel);
}
if (cADModel_B_List.Count() == 2)
{
cADModel_B_List.Add(CadModel_Main);
CADModelList_Second_B.Add(cADModel_B_List);
}
else
{
UpBeamCAdModel.Add(CadModel_Main);
}
}
TaskDialog.Show("1", FailCadModel_B.Count().ToString());
//第二组B成功部分生成
foreach (var CAdMidelList in CADModelList_Second_B)
{
CADModel cADModel_A = CAdMidelList[0];
CADModel cADModel_B = CAdMidelList[1];
CADModel cADModel_Main = CAdMidelList[2];
Line line = cADModel_Main.curveArray.get_Item(0) as Line;
List MinDistance = new List();//最短距离
List MinDistanceB = new List();//最短距离B
XYZ OnePoint = new XYZ();
XYZ TwoPont = new XYZ();
var cADModel_A_StratPoint = cADModel_A.curveArray.get_Item(0).GetEndPoint(0);
var cADModel_A_EndPoint = cADModel_A.curveArray.get_Item(0).GetEndPoint(1);
double distanceA = line.Distance(cADModel_A.location);
var cADModel_B_StratPoint = cADModel_B.curveArray.get_Item(0).GetEndPoint(0);
var cADModel_B_EndPoint = cADModel_B.curveArray.get_Item(0).GetEndPoint(1);
double distanceB = line.Distance(cADModel_B.location);
var cADModel_Main_StratPoint = cADModel_Main.curveArray.get_Item(0).GetEndPoint(0);
var cADModel_Main_EndPoint = cADModel_Main.curveArray.get_Item(0).GetEndPoint(1);
double A1 = cADModel_A_StratPoint.DistanceTo(cADModel_Main_StratPoint);
MinDistance.Add(A1);
double A2 = cADModel_A_StratPoint.DistanceTo(cADModel_Main_EndPoint);
MinDistance.Add(A2);
double A3 = cADModel_A_EndPoint.DistanceTo(cADModel_Main_StratPoint);
MinDistance.Add(A3);
double A4 = cADModel_A_EndPoint.DistanceTo(cADModel_Main_EndPoint);
MinDistance.Add(A4);
double MainDistance_A = MinDistance.Min();
int index_A = MinDistance.IndexOf(MainDistance_A);
switch (index_A)
{
case 0:
OnePoint = cADModel_A_StratPoint;
break;
case 1:
OnePoint = cADModel_A_StratPoint;
break;
case 2:
OnePoint = cADModel_A_EndPoint;
break;
case 3:
OnePoint = cADModel_A_EndPoint;
break;
}
double B1 = cADModel_B_StratPoint.DistanceTo(cADModel_Main_StratPoint);
MinDistanceB.Add(B1);
double B2 = cADModel_B_StratPoint.DistanceTo(cADModel_Main_EndPoint);
MinDistanceB.Add(B2);
double B3 = cADModel_B_EndPoint.DistanceTo(cADModel_Main_StratPoint);
MinDistanceB.Add(B3);
double B4 = cADModel_B_EndPoint.DistanceTo(cADModel_Main_EndPoint);
MinDistanceB.Add(B4);
double MainDistance_b = MinDistanceB.Min();
int index_B = MinDistanceB.IndexOf(MainDistance_b);
switch (index_B)
{
case 0:
TwoPont = cADModel_B_StratPoint;
break;
case 1:
TwoPont = cADModel_B_StratPoint;
break;
case 2:
TwoPont = cADModel_B_EndPoint;
break;
case 3:
TwoPont = cADModel_B_EndPoint;
break;
}
MinDistance.Clear();
MinDistanceB.Clear();
XYZ ChangeXYZ = new XYZ();
var LineLength = (GetMiddlePoint(OnePoint, cADModel_Main_StratPoint)).DistanceTo(GetMiddlePoint(TwoPont, cADModel_Main_EndPoint));
if (LineLength < 0.00328)//梁的2段线起点非同一端。2段线非同一长度,又非同一端的,中间点的误差选择为1mm
{
ChangeXYZ = TwoPont;
TwoPont = OnePoint;
OnePoint = ChangeXYZ;
}
Curve curve = Line.CreateBound((GetMiddlePoint(OnePoint, cADModel_Main_StratPoint)), GetMiddlePoint(TwoPont, cADModel_Main_EndPoint));
//TaskDialog.Show("1", OnePoint.ToString() + "
" + TwoPont.ToString());
//TaskDialog.Show("1", curve.GetEndPoint(0).ToString() + "
" + curve.GetEndPoint(1).ToString());
double distance = MainDistance_A;//cADModel_A.location.DistanceTo(cADModel_Main.location);
distance = Math.Round(distance * 304.8, 1);//作为梁_b的参数
string beamName = "ZBIM矩形梁 " + (float)(distance) + "*" + (float)(600) + "mm";//类型名 宽度*高度
if (!familSymbol_exists(beamName, "ZBIM - 矩形梁", doc))
{
MakeBeamType(beamName, "ZBIM - 矩形梁");
EditBeamType(beamName, (float)(distance), (float)(600));
}
using (Transaction transaction = new Transaction(doc))
{
transaction.Start("Beadm Strart Bulid" + tranNumber.ToString());
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_StructuralFraming);
foreach (FamilySymbol beamType in collector)
{
if (beamType.Name == beamName)
{
if (!beamType.IsActive)
{
beamType.Activate();
}
FamilyInstance beamInstance = doc.Create.NewFamilyInstance(curve, beamType, level, StructuralType.Beam);
var Elevation = beamInstance.get_Parameter(BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM);
//Elevation.Set(LevelFilter(doc,"标高 1").Id);
//Parameter bottom = column.LookupParameter("底部偏移");
//bottom.Set(UnitUtils.ConvertToInternalUnits(offsetElev, DisplayUnitType.DUT_MILLIMETERS));
//Parameter top = column.LookupParameter("顶部偏移");
//top.Set(UnitUtils.ConvertToInternalUnits(high, DisplayUnitType.DUT_MILLIMETERS));
break;
}
}
//foreach()
transaction.Commit();
}
//i++;
}
//第二组B的失败部分生成
foreach (var CAdMidelList in FailCadModel_B)
{
CADModel cADModel_A = CAdMidelList[0];
CADModel cADModel_Main = CAdMidelList[1];
Line line = cADModel_Main.curveArray.get_Item(0) as Line;
List MinDistance = new List();
XYZ OnePoint = new XYZ();
XYZ TwoPoint;//用于编造第二点
var cADModel_A_StratPoint = cADModel_A.curveArray.get_Item(0).GetEndPoint(0);
var cADModel_A_EndPoint = cADModel_A.curveArray.get_Item(0).GetEndPoint(1);
var cADModel_Main_StratPoint = cADModel_Main.curveArray.get_Item(0).GetEndPoint(0);
var cADModel_Main_EndPoint = cADModel_Main.curveArray.get_Item(0).GetEndPoint(1);
double A1 = cADModel_A_StratPoint.DistanceTo(cADModel_Main_StratPoint);
MinDistance.Add(A1);
double A2 = cADModel_A_StratPoint.DistanceTo(cADModel_Main_EndPoint);
MinDistance.Add(A2);
double A3 = cADModel_A_EndPoint.DistanceTo(cADModel_Main_StratPoint);
MinDistance.Add(A3);
double A4 = cADModel_A_EndPoint.DistanceTo(cADModel_Main_EndPoint);
MinDistance.Add(A4);
double MainDistance_A = MinDistance.Min();
int index_A = MinDistance.IndexOf(MainDistance_A);
switch (index_A)
{
case 0:
OnePoint = cADModel_A_StratPoint;
break;
case 1:
OnePoint = cADModel_A_StratPoint;
break;
case 2:
OnePoint = cADModel_A_EndPoint;
break;
case 3:
OnePoint = cADModel_A_EndPoint;
break;
}
if ((OnePoint.Y - cADModel_Main_StratPoint.Y) < 0.001)
{
TwoPoint = new XYZ(OnePoint.X, cADModel_Main_EndPoint.Y, OnePoint.Z);
}
else if ((OnePoint.Y - cADModel_Main_EndPoint.Y) < 0.001)
{
TwoPoint = new XYZ(OnePoint.X, cADModel_Main_StratPoint.Y, OnePoint.Z);
}
else
{
continue;
}
XYZ ChangeXYZ = new XYZ();
var LineLength = (GetMiddlePoint(OnePoint, cADModel_Main_StratPoint)).DistanceTo(GetMiddlePoint(TwoPoint, cADModel_Main_EndPoint));
if (LineLength < 0.00328)//梁的2段线起点非同一端。2段线非同一长度,又非同一端的,中间点的误差选择为1mm
{
ChangeXYZ = TwoPoint;
TwoPoint = OnePoint;
OnePoint = ChangeXYZ;
}
Curve curve = Line.CreateBound((GetMiddlePoint(OnePoint, cADModel_Main_StratPoint)), GetMiddlePoint(TwoPoint, cADModel_Main_EndPoint));
double distance = MainDistance_A;//cADModel_A.location.DistanceTo(cADModel_Main.location);
distance = Math.Round(distance * 304.8, 1);//作为梁_b的参数
string beamName = "ZBIM矩形梁 " + (float)(distance) + "*" + (float)(600) + "mm";//类型名 宽度*高度
if (!familSymbol_exists(beamName, "ZBIM - 矩形梁", doc))
{
MakeBeamType(beamName, "ZBIM - 矩形梁");
EditBeamType(beamName, (float)(distance), (float)(600));
}
using (Transaction transaction = new Transaction(doc))
{
transaction.Start("Beadm Strart Bulid" + tranNumber.ToString());
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_StructuralFraming);
foreach (FamilySymbol beamType in collector)
{
if (beamType.Name == beamName)
{
if (!beamType.IsActive)
{
beamType.Activate();
}
FamilyInstance beamInstance = doc.Create.NewFamilyInstance(curve, beamType, level, StructuralType.Beam);
var Elevation = beamInstance.get_Parameter(BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM);
break;
}
}
transaction.Commit();
}
}
}
return Result.Succeeded;
}
///
/// 取得所有同一图层的所有线
///
/// revit系统文档
/// 几何元素
/// 几何元素ID
///
private List getCurveArray(Document doc, GeometryElement geoElem, ElementId graphicsStyleId)
{
List curveArray_List = new List();
TransactionGroup transGroup = new TransactionGroup(doc, "绘制模型线");
transGroup.Start();
//判断元素类型
foreach (var gObj in geoElem)
{
GeometryInstance geomInstance = gObj as GeometryInstance;
//坐标转换。如果选择的是“自动-中心到中心”,或者移动了importInstance,需要进行坐标转换
Transform transform = geomInstance.Transform;
if (null != geomInstance)
{
foreach (var insObj in geomInstance.SymbolGeometry)//取几何得类别
{
if (insObj.GraphicsStyleId.IntegerValue != graphicsStyleId.IntegerValue)
continue;
if (insObj.GetType().ToString() == "Autodesk.Revit.DB.NurbSpline")
{
//不需要
}
if (insObj.GetType().ToString() == "Autodesk.Revit.DB.Line")
{
Line line = insObj as Line;
XYZ normal = XYZ.BasisZ;
XYZ point = line.GetEndPoint(0);
point = transform.OfPoint(point);
Line newLine = TransformLine(transform, line);
CurveArray curveArray = new CurveArray();
curveArray.Append(TransformLine(transform, line));
XYZ startPoint = newLine.GetEndPoint(0);
XYZ endPoint = newLine.GetEndPoint(1);
XYZ MiddlePoint = GetMiddlePoint(startPoint, endPoint);
double angle = (startPoint.Y - endPoint.Y) / startPoint.DistanceTo(endPoint);
double rotation = Math.Asin(angle);
CADModel cADModel = new CADModel();
cADModel.curveArray = curveArray;
cADModel.length = newLine.Length;
cADModel.shape = "矩形梁";
cADModel.width = 300/304.8;
cADModel.location = MiddlePoint;
cADModel.rotation = rotation;
curveArray_List.Add(cADModel);
}
if (insObj.GetType().ToString() == "Autodesk.Revit.DB.Arc")
{
//不需要
}
//对于连续的折线
if (insObj.GetType().ToString() == "Autodesk.Revit.DB.PolyLine")
{
PolyLine polyLine = insObj as PolyLine;
IList points = polyLine.GetCoordinates();
for (int i = 0; i < points.Count - 1; i++)
{
Line line = Line.CreateBound(points[i], points[i + 1]);
line = TransformLine(transform, line);
Line newLine = line;
CurveArray curveArray = new CurveArray();
curveArray.Append(newLine);
XYZ startPoint = newLine.GetEndPoint(0);
XYZ endPoint = newLine.GetEndPoint(1);
XYZ MiddlePoint = GetMiddlePoint(startPoint, endPoint);
double angle = (startPoint.Y - endPoint.Y) / startPoint.DistanceTo(endPoint);
double rotation = Math.Asin(angle);
CADModel cADModel = new CADModel();
cADModel.curveArray = curveArray;
cADModel.length = newLine.Length;
cADModel.shape = "矩形梁";
cADModel.width = 300 / 304.8;
cADModel.location = MiddlePoint;
cADModel.rotation = rotation;
curveArray_List.Add(cADModel);
//curveArray.Append(line);
}
//XYZ normal = XYZ.BasisZ;
//XYZ point = points.First();
//point = transform.OfPoint(point);
//CreateModelCurveArray(curveArray, normal, point);
}