using Autodesk.Revit.DB; using Autodesk.Revit.DB.Mechanical; using Autodesk.Revit.DB.Plumbing; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; using KDCS.Utils; using KMBIM.Revit.Tools.LogicClass; using KMBIM.Revit.Tools.Utils; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using System.Xml.Serialization; namespace KMBIM.Revit.Tools.Cmd.Insulation { [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 InsulationSettingsCommand : IExternalCommand { List pipeCollection = null; List ductCollection = null; List PipeinsulationCollection = null; List DuctinsulationCollection = null; TblDataStorage PipeData = new TblDataStorage(); TblInsulationType PipeinsulList = new TblInsulationType(); TblInsulationType DuctinsulList = new TblInsulationType(); IList ielement = null; bool bKeepInsulation = false; bool bApplyPipe = false; bool bApplyDuct = false; Document doc = null; UIDocument uidoc = null; public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { if (!WorkMain.GetInstance().IsValid) return Autodesk.Revit.UI.Result.Succeeded; // The location of this command assembly string currentCommandAssemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location; UIApplication uiapp = commandData.Application; uidoc = uiapp.ActiveUIDocument; Autodesk.Revit.ApplicationServices.Application app = uiapp.Application; doc = uidoc.Document; Autodesk.Revit.DB.View view = doc.ActiveView; Autodesk.Revit.Creation.Application creApp = uiapp.Application.Create; Autodesk.Revit.Creation.Document creDoc = doc.Create; PipeinsulationCollection = new FilteredElementCollector(doc) .OfClass(typeof(PipeInsulationType)) .OfType() .ToList(); DuctinsulationCollection = new FilteredElementCollector(doc) .OfClass(typeof(DuctInsulationType)) .OfType() .ToList(); if (PipeinsulationCollection.Count() <= 0) { System.Windows.Forms.MessageBox.Show("파이프 보온재 타입이 없습니다. 하나이상 추가후 시도 하시기 바랍니다."); return Result.Failed; ; } if (DuctinsulationCollection.Count() <= 0) { System.Windows.Forms.MessageBox.Show("덕트 보온재 타입이 없습니다. 하나이상 추가후 시도 하시기 바랍니다."); return Result.Failed; ; } // Looking for the DuctType ductCollection = new FilteredElementCollector(doc) .OfClass(typeof(DuctType)) .OfType() .ToList(); // Looking for the PipeType pipeCollection = new FilteredElementCollector(doc) .OfClass(typeof(PipeType)) .OfType() .ToList(); foreach (var row in PipeinsulationCollection) { PipeinsulList.Add(row.Name); } string strPipeinsul = string.Empty; if (PipeinsulationCollection.Count > 0) strPipeinsul = PipeinsulationCollection.First().Name; foreach (var row in DuctinsulationCollection) { DuctinsulList.Add(row.Name); } string strDuctinsul = string.Empty; if (DuctinsulationCollection.Count >0) strDuctinsul = DuctinsulationCollection.First().Name; // 선택 ielement = uidoc.Selection.PickElementsByRectangle("단열재를 정의할 배관 또는 덕트 선택:"); ; if (ielement == null) return Result.Succeeded; open(); if (PipeData.m_table.Count == 0) { KDCS.Utils.Reg.setReg("kdcsInsulationDefaultType", "기본"); PipeData.AddCategory("기본"); if (PipeData.GetTable("기본", true).Count == 0) { foreach (var row in pipeCollection) { PipeData.Add("기본", true, row.Name, strPipeinsul, 25.0); var sizes = Util.GetPipeSegmentSizes(doc, row.Name); foreach(var r in sizes) { PipeData.Find("기본", true, row.Name).BoreList.Add(new PipeDiameterOfThickness((int)r, 25)); } } } foreach (var row in ductCollection) { PipeData.Add("기본", false, row.Name, strDuctinsul, 25.0); } } else { List delPipeData = new List(); List delDuctData = new List(); //현재 프로젝트에는 없는데 저장된 파일에는 있는것을 삭제 하자. foreach (var category in PipeData.GetTable()) { foreach (var row in category.Value["pipe"]) { //subcategory.Key if (pipeCollection.Find(x => x.Name == row.strType) == null) { if (delPipeData.Find(x => x.strType == row.strType) == null) delPipeData.Add(row); } } } foreach (var category in PipeData.GetTable()) { foreach (var row in category.Value["duct"]) { //subcategory.Key if (ductCollection.Find(x => x.Name == row.strType) == null) { if (delDuctData.Find(x => x.strType == row.strType) == null) delDuctData.Add(row); } } } //삭제하기 foreach (var pipe in delPipeData) { foreach (var category in PipeData.GetTable()) { PipeData.Remove(category.Key, true, pipe.strType); } } foreach (var duct in delDuctData) { foreach (var category in PipeData.GetTable()) { PipeData.Remove(category.Key, false, duct.strType); } } //현재 프로젝트에는 있는데 저장된 파일에는 없는 경우 추가한다. List newPipeData = new List(); List newDuctData = new List(); foreach (var pipe in pipeCollection) { if (PipeData.m_table.First().Value["pipe"].Find(x => x.strType == pipe.Name) == null) { newPipeData.Add(new DataStorage(pipe.Name, strPipeinsul, 25, true)); } } foreach (var duct in ductCollection) { if (PipeData.m_table.First().Value["duct"].Find(x => x.strType == duct.Name) == null) { newDuctData.Add(new DataStorage(duct.Name, strPipeinsul, 25, true)); } } //추가하기 foreach (var pipe in newPipeData) { foreach (var category in PipeData.GetTable()) { PipeData.Add(category.Key, true, pipe.strType, pipe.strLaggingType, pipe.dblLaggingThickness, pipe.bisNew); var sizes = Util.GetPipeSegmentSizes(doc, pipe.strType); foreach (var r in sizes) { PipeData.Find("기본", true, pipe.strType).BoreList.Add(new PipeDiameterOfThickness((int)r, (int)pipe.dblLaggingThickness)); } } } foreach (var duct in newDuctData) { foreach (var category in PipeData.GetTable()) { PipeData.Add(category.Key, false, duct.strType, duct.strLaggingType, duct.dblLaggingThickness, duct.bisNew); } } } InsulationSettings dlg = new InsulationSettings(doc, uidoc, pipeCollection, ductCollection, PipeinsulationCollection, DuctinsulationCollection, PipeData, ref PipeinsulList, ref DuctinsulList); if (dlg.ShowDialog() == DialogResult.OK) { this.PipeData = dlg.PipeData; this.bKeepInsulation = dlg.bKeepInsulation; this.bApplyPipe = dlg.bApplyPipe; this.bApplyDuct = dlg.bApplyDuct; save(); using (Transaction tr = new Transaction(doc)) { tr.Start("Insulation"); go(); tr.Commit(); } } return Result.Succeeded; } public void go() { string category = KDCS.Utils.Reg.getReg("kdcsInsulationDefaultType"); if (ielement == null) return; foreach (Element el in ielement.ToList()) { Type et = el.GetType(); if (typeof(RevitLinkInstance) == et) { //linkInstance = e as RevitLinkInstance; continue; } if (el is Pipe) { if(bApplyPipe) SetInsulation(el); } else if (el is Duct) { if (bApplyDuct) SetInsulation(el); }//end-if else if (el is FamilyInstance) { SetInsulation(el); } }//end-foreach } public Element GetPipeType(string strPipeType) { foreach (var item in pipeCollection) { if (item.Name.CompareTo(strPipeType) == 0) { return item; } } return null; } public Element GetPipeInsulationType(string strInsulationType) { foreach (var item in PipeinsulationCollection) { if (item.Name.CompareTo(strInsulationType) == 0) { return item; } } return null; } public Element GetDuctInsulationType(string strInsulationType) { foreach (var item in DuctinsulationCollection) { if (item.Name.CompareTo(strInsulationType) == 0) { return item; } } return null; } public void save() { string location = Util.GetKMBIMLibraryFolder("\\Libraries\\Insulation"); string jsonPath = Path.Combine(location, "settings.kins"); //DirectoryInfo 생성 DirectoryInfo di = new DirectoryInfo(location); //DirectoryInfo.Exists로 폴더 존재유무 확인 if (!di.Exists) { di.Create(); } using (StreamWriter file = File.CreateText(jsonPath)) { string strJson = PipeData.toJson(); JsonSerializer serializer = new JsonSerializer(); //serialize object directly into file stream serializer.Serialize(file, strJson); } } public void open() { string location = Util.GetKMBIMLibraryFolder("\\Libraries\\Insulation"); string jsonPath = Path.Combine(location, "settings.kins"); //DirectoryInfo 생성 DirectoryInfo di = new DirectoryInfo(location); //DirectoryInfo.Exists로 폴더 존재유무 확인 if (!di.Exists) { di.Create(); } if (File.Exists(jsonPath)) { using (StreamReader r = new StreamReader(jsonPath)) { using (JsonTextReader reader1 = new JsonTextReader(r)) { JValue o2 = (JValue)JToken.ReadFrom(reader1); string strJson = o2.ToString(); PipeData.LoadJson(strJson); } } } } /// /// /// /// public void SetInsulation(Element el) { string category = KDCS.Utils.Reg.getReg("kdcsInsulationDefaultType"); if (category.CompareTo("") == 0) { return; } if (el is Pipe) { if (!bApplyPipe) return; ICollection insulationIds = InsulationLiningBase.GetInsulationIds(doc, el.Id); Pipe pipe = el as Pipe; if (insulationIds.Count > 0) { foreach (ElementId insulationId in insulationIds) { Element insEl = doc.GetElement(insulationId); Autodesk.Revit.DB.Plumbing.PipeInsulation insulation = insEl as Autodesk.Revit.DB.Plumbing.PipeInsulation; if (insulation != null) { if (GetPipeInsulationType(PipeData.Find(category, true, pipe.PipeType.Name).strLaggingType).Id != null) { Autodesk.Revit.DB.Parameter pram = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM); double dia = Unit.FeetToMM(pram.AsDouble()); var tmp = PipeData.Find(category, true, pipe.PipeType.Name).BoreList.Find(i => i.nDiameter == dia); if (tmp != null) { if(tmp.dblThickness >0) { insulation.ChangeTypeId(GetPipeInsulationType(PipeData.Find(category, true, pipe.PipeType.Name).strLaggingType).Id); insulation.Thickness = Unit.MMToFeet(tmp.dblThickness); } else { doc.Delete(insulationId); } } } } } } else { if (GetPipeInsulationType(PipeData.Find(category, true, pipe.PipeType.Name).strLaggingType).Id != null) { Autodesk.Revit.DB.Parameter pram = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM); double dia = Unit.FeetToMM(pram.AsDouble()); var tmp = PipeData.Find(category, true, pipe.PipeType.Name).BoreList.Find(i => i.nDiameter == dia); if (tmp != null) { if(tmp.dblThickness > 0) { PipeInsulation.Create(doc, el.Id, GetPipeInsulationType(PipeData.Find(category, true, pipe.PipeType.Name).strLaggingType).Id, Unit.MMToFeet(tmp.dblThickness)); } } } } } else if (el is Duct) { if (!bApplyDuct) return; ICollection insulationIds = InsulationLiningBase.GetInsulationIds(doc, el.Id); Duct duct = el as Duct; if (insulationIds.Count > 0) { foreach (ElementId insulationId in insulationIds) { Element insEl = doc.GetElement(insulationId); Autodesk.Revit.DB.Mechanical.DuctInsulation insulation = insEl as Autodesk.Revit.DB.Mechanical.DuctInsulation; if (insulation != null) { if (GetDuctInsulationType(PipeData.Find(category, false, duct.DuctType.Name).strLaggingType).Id != null) { double dblThickness = PipeData.Find(category, false, duct.DuctType.Name).dblLaggingThickness; if (dblThickness > 0) { insulation.ChangeTypeId(GetDuctInsulationType(PipeData.Find(category, false, duct.DuctType.Name).strLaggingType).Id); insulation.Thickness = Unit.MMToFeet(PipeData.Find(category, false, duct.DuctType.Name).dblLaggingThickness); } else { doc.Delete(insulationId); } } } } } else { string strLaggingType = PipeData.Find(category, false, duct.DuctType.Name).strLaggingType; Element InsulType = GetDuctInsulationType(strLaggingType); if (InsulType.Id != null) { double dblThickness = PipeData.Find(category, false, duct.DuctType.Name).dblLaggingThickness; if (dblThickness > 0) { DuctInsulation.Create(doc, el.Id, InsulType.Id, Unit.MMToFeet(dblThickness)); } } } }//end-if else if (el is FamilyInstance) { Duct duct = null; Pipe pipe = null; FamilyInstance familyInstance = el as FamilyInstance; if (familyInstance.Category.Id.IntegerValue == (int)BuiltInCategory.OST_DuctFitting || familyInstance.Category.Id.IntegerValue == (int)BuiltInCategory.OST_PipeAccessory || familyInstance.Category.Id.IntegerValue == (int)BuiltInCategory.OST_PipeFitting || familyInstance.Category.Id.IntegerValue == (int)BuiltInCategory.OST_DuctAccessory) { ICollection insulationIds = InsulationLiningBase.GetInsulationIds(doc, el.Id); var ConnectorList = Util.GetElementConnectors(el); foreach (var item in ConnectorList) { Connector connect = Util.GetNextElementConnector(item); if (connect == null) continue; if (connect.Owner != null) { if (connect.Owner is Duct) { duct = connect.Owner as Duct; break; } if (connect.Owner is Pipe) { pipe = connect.Owner as Pipe; break; } } } } if (familyInstance.Category.Id.IntegerValue == (int)BuiltInCategory.OST_DuctFitting) { if (!bApplyDuct) return; if (duct == null) return; ICollection insulationIds = InsulationLiningBase.GetInsulationIds(doc, el.Id); if (insulationIds.Count > 0) { if (bKeepInsulation) return; foreach (ElementId insulationId in insulationIds) { Element insEl = doc.GetElement(insulationId); Autodesk.Revit.DB.Mechanical.DuctInsulation insulation = insEl as Autodesk.Revit.DB.Mechanical.DuctInsulation; if (insulation != null) { if (GetDuctInsulationType(PipeData.Find(category, false, duct.DuctType.Name).strLaggingType).Id != null) { double dblThickness = PipeData.Find(category, false, duct.DuctType.Name).dblLaggingThickness; if (dblThickness > 0) { insulation.ChangeTypeId(GetDuctInsulationType(PipeData.Find(category, false, duct.DuctType.Name).strLaggingType).Id); insulation.Thickness = Unit.MMToFeet(dblThickness); } else { doc.Delete(insulationId); } } } } } else { string strLaggingType = PipeData.Find(category, false, duct.DuctType.Name).strLaggingType; Element InsulType = GetDuctInsulationType(strLaggingType); if (InsulType.Id != null) { double dblThickness = PipeData.Find(category, false, duct.DuctType.Name).dblLaggingThickness; if (dblThickness > 0) { DuctInsulation.Create(doc, el.Id, InsulType.Id, Unit.MMToFeet(dblThickness)); } } } } else if (familyInstance.Category.Id.IntegerValue == (int)BuiltInCategory.OST_PipeAccessory) { if (!bApplyPipe) return; if (pipe == null) return; ICollection insulationIds = InsulationLiningBase.GetInsulationIds(doc, el.Id); if (insulationIds.Count > 0) { if (bKeepInsulation) return; foreach (ElementId insulationId in insulationIds) { Element insEl = doc.GetElement(insulationId); Autodesk.Revit.DB.Plumbing.PipeInsulation insulation = insEl as Autodesk.Revit.DB.Plumbing.PipeInsulation; if (insulation != null) { if (GetPipeInsulationType(PipeData.Find(category, true, pipe.PipeType.Name).strLaggingType).Id != null) { Autodesk.Revit.DB.Parameter pram = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM); double dia = Unit.FeetToMM(pram.AsDouble()); var tmp = PipeData.Find(category, true, pipe.PipeType.Name).BoreList.Find(i => i.nDiameter == dia); if (tmp != null) { if (tmp.dblThickness > 0) { insulation.ChangeTypeId(GetPipeInsulationType(PipeData.Find(category, true, pipe.PipeType.Name).strLaggingType).Id); insulation.Thickness = Unit.MMToFeet(tmp.dblThickness); } else { doc.Delete(insulationId); } } } } } } else { string strLaggingType = PipeData.Find(category, true, pipe.PipeType.Name).strLaggingType; Element InsulType = GetPipeInsulationType(strLaggingType); if (InsulType.Id != null) { Autodesk.Revit.DB.Parameter pram = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM); double dia = Unit.FeetToMM(pram.AsDouble()); var tmp = PipeData.Find(category, true, pipe.PipeType.Name).BoreList.Find(i => i.nDiameter == dia); if (tmp != null) { if (tmp.dblThickness > 0) { PipeInsulation.Create(doc, el.Id, InsulType.Id, Unit.MMToFeet(tmp.dblThickness)); } } } } } else if (familyInstance.Category.Id.IntegerValue == (int)BuiltInCategory.OST_PipeFitting) { if (!bApplyPipe) return; if (pipe == null) return; ICollection insulationIds = InsulationLiningBase.GetInsulationIds(doc, el.Id); if (insulationIds.Count > 0) { if (bKeepInsulation) return; foreach (ElementId insulationId in insulationIds) { Element insEl = doc.GetElement(insulationId); Autodesk.Revit.DB.Plumbing.PipeInsulation insulation = insEl as Autodesk.Revit.DB.Plumbing.PipeInsulation; if (insulation != null) { if (GetPipeInsulationType(PipeData.Find(category, true, pipe.PipeType.Name).strLaggingType).Id != null) { Autodesk.Revit.DB.Parameter pram = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM); double dia = Unit.FeetToMM(pram.AsDouble()); var tmp = PipeData.Find(category, true, pipe.PipeType.Name).BoreList.Find(i => i.nDiameter == dia); if (tmp != null) { if (tmp.dblThickness > 0) { insulation.ChangeTypeId(GetPipeInsulationType(PipeData.Find(category, true, pipe.PipeType.Name).strLaggingType).Id); insulation.Thickness = Unit.MMToFeet(tmp.dblThickness); } else { doc.Delete(insulationId); } } } } } } else { string strLaggingType = PipeData.Find(category, true, pipe.PipeType.Name).strLaggingType; Element InsulType = GetPipeInsulationType(strLaggingType); if (InsulType.Id != null) { Autodesk.Revit.DB.Parameter pram = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM); double dia = Unit.FeetToMM(pram.AsDouble()); var tmp = PipeData.Find(category, true, pipe.PipeType.Name).BoreList.Find(i => i.nDiameter == dia); if (tmp != null) { if (tmp.dblThickness > 0) { PipeInsulation.Create(doc, el.Id, InsulType.Id, Unit.MMToFeet(tmp.dblThickness)); } } } } } else if (familyInstance.Category.Id.IntegerValue == (int)BuiltInCategory.OST_DuctAccessory) { if (!bApplyDuct) return; if (duct == null) return; ICollection insulationIds = InsulationLiningBase.GetInsulationIds(doc, el.Id); if (insulationIds.Count > 0) { if (bKeepInsulation) return; foreach (ElementId insulationId in insulationIds) { Element insEl = doc.GetElement(insulationId); Autodesk.Revit.DB.Mechanical.DuctInsulation insulation = insEl as Autodesk.Revit.DB.Mechanical.DuctInsulation; if (insulation != null) { if (GetDuctInsulationType(PipeData.Find(category, false, duct.DuctType.Name).strLaggingType).Id != null) { double dblThickness = PipeData.Find(category, false, duct.DuctType.Name).dblLaggingThickness; if (dblThickness > 0) { insulation.ChangeTypeId(GetDuctInsulationType(PipeData.Find(category, false, duct.DuctType.Name).strLaggingType).Id); insulation.Thickness = Unit.MMToFeet(dblThickness); } else { doc.Delete(insulationId); } } } } } else { string strLaggingType = PipeData.Find(category, false, duct.DuctType.Name).strLaggingType; Element InsulType = GetDuctInsulationType(strLaggingType); if (InsulType.Id != null) { double dblThickness = PipeData.Find(category, false, duct.DuctType.Name).dblLaggingThickness; if (dblThickness > 0) { DuctInsulation.Create(doc, el.Id, InsulType.Id, Unit.MMToFeet(dblThickness)); } } } } } } } }