public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { var uidoc = commandData.Application.ActiveUIDocument; var doc = uidoc.Document; var sel = uidoc.Selection; var acview = uidoc.ActiveView; if (!(acview is ViewPlan)) { MessageBox.Show("请到平面视图执行此命令"); return(Result.Cancelled); } Category equipmentCategory = Category.GetCategory(doc, BuiltInCategory.OST_FireAlarmDevices); //电气设备集合 ElementCategoryFilter catefilter1 = new ElementCategoryFilter(BuiltInCategory.OST_FireAlarmDevices); ElementCategoryFilter catefilter2 = new ElementCategoryFilter(BuiltInCategory.OST_ElectricalFixtures); ElementCategoryFilter catefilter3 = new ElementCategoryFilter(BuiltInCategory.OST_ElectricalEquipment); var filterlist = new List <ElementFilter>() { catefilter3, catefilter2, catefilter1 }; LogicalOrFilter ElectricaldeviceOrfixturesFilter = new LogicalOrFilter(filterlist); var elecEquipCollector = new FilteredElementCollector(doc, acview.Id).WherePasses(ElectricaldeviceOrfixturesFilter).WhereElementIsNotElementType().Cast <FamilyInstance>().ToList(); var fsInfoCol = new List <FsInfo>(); //LogHelper.LogWrite(); //MessageBox.Show("sec1:" + elecEquipCollector.Count); var namelist = elecEquipCollector.Select(m => m.Symbol.FamilyName); var namestring = string.Join("\n", namelist); //MessageBox.Show(namestring); foreach (FamilyInstance fs in elecEquipCollector) { var fsinfo = new FsInfo(fs); fsInfoCol.Add(fsinfo); } //电线折线集合 var wirecollector = new FilteredElementCollector(doc, acview.Id).OfClass(typeof(Wire)).Cast <Wire>().ToList(); var wirelineCol = new List <Line>(); foreach (Wire wire in wirecollector) { var lines = getwirelines(wire); wirelineCol.AddRange(lines); } //MessageBox.Show("sec2:" + wirecollector.Count); var wiretypes = doc.TCollector <WireType>(); string wiretypename = "DLcoverwiretype"; var coverwiretype = default(WireType); bool targetWireTypeexist = false; //var wiretypenames = wiretypes.Select(m => m.Name); coverwiretype = wiretypes.Where(m => m.Name == wiretypename)?.FirstOrDefault(); if (coverwiretype != null) { targetWireTypeexist = true; } var firswiretype = wiretypes.First(); TransactionGroup tsg = new TransactionGroup(doc, "创建电缆遮挡"); tsg.Start(); Transaction ts1 = new Transaction(doc, "创建电缆类型"); ts1.Start(); if (!targetWireTypeexist) { coverwiretype = firswiretype.Duplicate(wiretypename) as WireType; } ts1.Commit(); Transaction ts2 = new Transaction(doc, "覆盖"); ts2.Start(); //辅助计数 var count = default(long); foreach (var fsinfo in fsInfoCol) { foreach (Line line in wirelineCol) { #region fortest //var temins = (fsinfo.Id.GetElement(doc) as FamilyInstance); //if (temins.Symbol.FamilyName.Contains("单相空调插座")) //{ // MessageBox.Show(fsinfo.Outline1.Count.ToString()); // drawoutline(fsinfo.Outline1, doc); // //MessageBox.Show(fsinfo.Width.ToString() + Environment.NewLine + fsinfo.Height.ToString()); //} #endregion //添加遮挡导线 if (lineIntersecOutline(line, fsinfo.Outline1)) { CreateHideWire(doc, fsinfo, line, coverwiretype, WiringType.Chamfer, acview); //绘制外轮廓 //drawoutline(fsinfo.Outline1, doc); } } } //MessageBox.Show("inbox count:" + count.ToString()); ts2.Commit(); tsg.Assimilate(); return(Result.Succeeded); }
public void CreateHideWire(Document doc, FsInfo insinfo, Line wireLine, WireType type, WiringType wiringtype, View view) { //var doc = ins.Document; var ins = insinfo.Id.GetElement(doc) as FamilyInstance; var locationpo = (ins.Location as LocationPoint).Point; var linedir = wireLine.Direction; if (insinfo.LocationInCenter)// IsLocationInCenter(ins) { var facingDir = ins.FacingOrientation; var handDir = ins.HandOrientation; var viewscale = view.Scale; var lengthRatio = viewscale / 50.0; var width = lengthRatio * insinfo.Width; //getWidth(ins); var height = lengthRatio * insinfo.Height; //getHeight(ins); var po1 = default(XYZ); // locationpo + height / 4 * handDir; var po2 = default(XYZ); //locationpo - height / 4 * handDir; if (linedir.AngleTo(handDir) < Math.PI / 4) { po1 = locationpo + width / 4 * handDir; po2 = locationpo - width / 4 * handDir; } else { po1 = locationpo + height / 4 * facingDir; po2 = locationpo - height / 4 * facingDir; } var polist = new List <XYZ>(); polist.Add(po1); polist.Add(locationpo); polist.Add(po2); //Wire.Create(doc, type.Id, view.Id, wiringtype,, null, null); foreach (var po in polist) { CreateHideWire(po, wireLine, type, wiringtype, view, 3); } } if (insinfo.LocationNearBottom)//IslocationNearBottom(ins)) { var facingDir = ins.FacingOrientation; var handDir = ins.HandOrientation; var viewscale = view.Scale; var lengthRatio = viewscale / 50.0; var width = lengthRatio * insinfo.Width; // getWidth(ins); var height = lengthRatio * insinfo.Height; // getHeight(ins); var po1 = default(XYZ); // locationpo + height / 4 * handDir; var po2 = default(XYZ); //locationpo - height / 4 * handDir; if (linedir.AngleTo(handDir) < Math.PI / 4) { po1 = locationpo + width / 4.0 * handDir; po2 = locationpo - width / 4.0 * handDir; } else { po1 = locationpo + height / 4 * facingDir; po2 = po1 + height / 4 * facingDir; } var polist = new List <XYZ>(); polist.Add(po1); polist.Add(locationpo); polist.Add(po2); //Wire.Create(doc, type.Id, view.Id, wiringtype,, null, null); foreach (var po in polist) { CreateHideWire(po, wireLine, type, wiringtype, view, 3); } } else { var facingDir = ins.FacingOrientation; var handDir = ins.HandOrientation; var viewscale = view.Scale; var lengthRatio = viewscale / 50.0; var width = lengthRatio * insinfo.Width; // getWidth(ins); var height = lengthRatio * insinfo.Height; // getHeight(ins); var po1 = default(XYZ); // locationpo + height / 4 * handDir; var po2 = default(XYZ); //locationpo - height / 4 * handDir; if (linedir.AngleTo(handDir) < Math.PI / 4) { po1 = locationpo + width / 4 * handDir; po2 = locationpo - width / 4 * handDir; } else { po1 = locationpo + height / 4 * facingDir; po2 = locationpo - height / 4 * facingDir; } var polist = new List <XYZ>(); polist.Add(po1); polist.Add(locationpo); polist.Add(po2); //Wire.Create(doc, type.Id, view.Id, wiringtype,, null, null); foreach (var po in polist) { CreateHideWire(po, wireLine, type, wiringtype, view, 3); } } }