//get specificaions regarding the dia and spacing public static List <FEMS_Specs> GetFEMS_Specs(specificationData configFileData) { List <FEMS_Specs> specList = new List <FEMS_Specs>(); FEMS_Specs specs = new FEMS_Specs(); try { if (File.Exists(configFileData.specsFile)) { //read all the contents of the file into a string array string[] fileContents = File.ReadAllLines(configFileData.specsFile); string[] splitLines = null; for (int i = 0; i < fileContents.Count(); i++) { splitLines = fileContents[i].Split(' '); if (splitLines[0] != "" && splitLines[1] != "") { specs.dia = double.Parse(splitLines[0].Trim()); specs.spacing = double.Parse(splitLines[1].Trim()); specList.Add(specs); } } } } catch { return(null); } return(specList); }
//default execute method required by the IExternalCommand class public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { try { //call to the security check method to check for authentication security = SecurityLNT.Security_Check(); if (security == false) { return(Result.Succeeded); } //read the config file specificationData configFileData = UtilityMethods.ReadConfig(); //check for discipline if (configFileData.discipline != "FEMS") { TaskDialog.Show("Failed!", "Sorry! Plug-in Not intended for your discipline."); return(Result.Succeeded); } //exception handled if (configFileData.offset == -1.0) { MessageBox.Show("Configuration data not found!"); return(Result.Succeeded); } //get all data from specification file specList = UtilityMethods.GetFEMS_Specs(configFileData); //exception handled if (specList == null) { MessageBox.Show("Specifications not found!"); return(Result.Succeeded); } //open the active document in revit m_document = commandData.Application.ActiveUIDocument; //get the selected element set eleSet = m_document.Selection.Elements; if (eleSet.IsEmpty) { MessageBox.Show("Please select pipes before executing the Add-in!"); return(Result.Succeeded); } //get family name from config data FamilyName = configFileData.selectedFamily; //check if family exists family = UtilityMethods.FindElementByName(m_document.Document, typeof(Family), FamilyName) as Family; //if existing if (family == null) { MessageBox.Show("Please load the family into the project and re-run the Add-in."); return(Result.Succeeded); } //get the 3D view to workon view3D = UtilityMethods.Get3D_View(m_document.Document); //exception handled if (view3D == null) { MessageBox.Show("No 3D view available!"); return(Result.Succeeded); } //set the 3D view sectionBox to false and copy its bounding box bounds = UtilityMethods.GetBounds(view3D); //exception handled if (bounds == null) { MessageBox.Show("Failed to generate bounding box!"); return(Result.Succeeded); } //get the family symbol symbol = UtilityMethods.GetFamilySymbol(family); //exception handled if (family == null) { MessageBox.Show("No family symbol for the family you specified!"); return(Result.Succeeded); } //iterate through all the selected elements foreach (Element ele in eleSet) { //check whether the selected element is of type pipe if (ele is Pipe) { //get the location curve of the pipe pipeCurve = ((LocationCurve)ele.Location).Curve; //if the length of pipe curve obtained is zero skip that pipe if (pipeCurve.Length == 0) { FailedToPlace.Add(ele.Id); continue; } //from the specification file, get the spacing corresponding to the pipe diameter spacing = UtilityMethods.GetSpacing(ele, specList); //check if the spacing returned is -1 if (spacing == -1) { FailedToPlace.Add(ele.Id); continue; } //get the points for placing the family instances points = UtilityMethods.GetPlacementPoints(spacing, pipeCurve, 1000 * configFileData.offset, 1000 * configFileData.minSpacing); //check if the points is null exception if (points == null) { FailedToPlace.Add(ele.Id); continue; } //get the pipe level pipeLevel = (Level)m_document.Document.GetElement(ele.LevelId); //iterate through all the points for placing the family instances foreach (XYZ point in points) { try { //create the instances at each points tempEle = m_document.Document.Create.NewFamilyInstance (point, symbol, ele, pipeLevel, StructuralType.NonStructural); createdElements.Add(tempEle.Id); } catch { FailedToPlace.Add(ele.Id); continue; } //find the rod length required by using the reference intersector class rodLength = UtilityMethods.ExtendRod(point, m_document.Document, view3D); if (rodLength == -1) { FailedToPlace.Add(ele.Id); createdElements.Remove(tempEle.Id); m_document.Document.Delete(tempEle.Id); continue; } //adjust the newly created element properties based on the rodlength, //orientation and dia of pipe if (!UtilityMethods.AdjustElement(m_document.Document, tempEle, point, (Pipe)ele, rodLength, pipeCurve)) { FailedToPlace.Add(ele.Id); createdElements.Remove(tempEle.Id); m_document.Document.Delete(tempEle.Id); continue; } } } } if (!UtilityMethods.SetBounds(bounds, view3D)) { MessageBox.Show("Sorry! Couldn't restore the sectionbox for the 3D view."); return(Result.Succeeded); } return(Result.Succeeded); } catch (Exception e) { UtilityMethods.SetBounds(bounds, view3D); message = e.Message; return(Autodesk.Revit.UI.Result.Failed); } throw new NotImplementedException(); }
//method to Read data from the configuration file public static specificationData ReadConfig() { specificationData fileData = new specificationData(); fileData.offset = -1.0; string configurationFile = null, assemblyLocation = null; try { //open configuration file assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location; //convert the active file path into directory name if (File.Exists(assemblyLocation)) { assemblyLocation = new FileInfo(assemblyLocation).Directory.ToString(); } //get parent directory of the current directory if (Directory.Exists(assemblyLocation)) { assemblyLocation = Directory.GetParent(assemblyLocation).ToString(); } } catch { fileData.offset = -1.0; return(fileData); } try { configurationFile = assemblyLocation + @"\Spacing Configuration\Configuration.txt"; if (!(File.Exists(configurationFile))) { MessageBox.Show("Configuration file doesn't exist!"); return(fileData); } else { //read all the contents of the file into a string array string[] fileContents = File.ReadAllLines(configurationFile); for (int i = 0; i < fileContents.Count(); i++) { if (fileContents[i].Contains("SelectedFamily: ")) { fileData.selectedFamily = fileContents[i].Replace("SelectedFamily: ", ""); } else if (fileContents[i].Contains("Discipline: ")) { fileData.discipline = fileContents[i].Replace("Discipline: ", ""); } else if (fileContents[i].Contains("Offest: ")) { fileData.offset = double.Parse(fileContents[i].Replace("Offest: ", "").Trim()); } else if (fileContents[i].Contains("Spacing: ")) { fileData.minSpacing = double.Parse(fileContents[i].Replace("Spacing: ", "").Trim()); } else if (fileContents[i].Contains("SupportType: ")) { fileData.supportType = fileContents[i].Replace("SupportType: ", ""); } else if (fileContents[i].Contains("File Location: ")) { fileData.specsFile = fileContents[i].Replace("File Location: ", ""); } } } } catch { fileData.offset = -1.0; return(fileData); } return(fileData); }
//default execute method required by the IExternalCommand class public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { try { //call to the security check method to check for authentication security = SecurityLNT.Security_Check(); if (security == false) { return(Result.Succeeded); } //get the application data Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application; //read the config file specificationData configFileData = UtilityMethods.ReadConfig(); //check for discipline if (configFileData.discipline != "FEMS") { TaskDialog.Show("Failed!", "Sorry! Plug-in Not intended for your discipline."); return(Result.Succeeded); } //exception handled if (configFileData.offset == -1.0) { MessageBox.Show("Configuration data not found!"); return(Result.Succeeded); } //get all data from specification file specList = UtilityMethods.GetFEMS_Specs(configFileData); //exception handled if (specList == null) { MessageBox.Show("Specifications not found!"); return(Result.Succeeded); } //open the active document in revit m_document = commandData.Application.ActiveUIDocument; //get the selected element set eleSet = m_document.Selection.Elements; if (eleSet.IsEmpty) { MessageBox.Show("Please select pipes before executing the Add-in!"); return(Result.Succeeded); } //call to method to get the transform required Transform transform = UtilityMethods.GetInverseTransform(m_document.Document, commandData.Application.Application); if (transform == null) { MessageBox.Show("Sorry! Couldn't find a possible transform."); return(Result.Succeeded); } //get family name from config data FamilyName = configFileData.selectedFamily; //check if family exists family = UtilityMethods.FindElementByName(m_document.Document, typeof(Family), FamilyName) as Family; //if existing if (family == null) { MessageBox.Show("Please load the family into the project and re-run the Add-in."); return(Result.Succeeded); } //get the family symbol symbol = UtilityMethods.GetFamilySymbol(family); //exception handled if (family == null) { MessageBox.Show("No family symbol for the family you specified!"); return(Result.Succeeded); } //create a logical filter for all the structural elements LogicalOrFilter filter = UtilityMethods.GetStructuralFilter(); //if no filter is returned if (filter == null) { return(Result.Succeeded); } //get the structural elements from the documents List <Element> structuralElements = new List <Element>(); structuralElements = UtilityMethods.GetStructuralElements(app, filter); if (structuralElements == null) { MessageBox.Show("Sorry! No structural element found"); return(Result.Succeeded); } //list to store all the planar botom faces of the structural elements List <PlanarFace> pFace = new List <PlanarFace>(); //find and add bottom planar faces to the list foreach (Element e in structuralElements) { PlanarFace pf = UtilityMethods.GetBottomFace(e); if (pf != null) { pFace.Add(pf); } } //clear the structural elements list as it is no longer required structuralElements.Clear(); //control variable for removing unwanted elements from the planar faces list int flag = 0; //iterate through all the selected elements foreach (Element ele in eleSet) { //check whether the selected element is of type pipe if (ele is Pipe) { //get the location curve of the pipe pipeCurve = ((LocationCurve)ele.Location).Curve; //if the length of pipe curve obtained is zero skip that pipe if (pipeCurve.Length == 0) { FailedToPlace.Add(ele.Id); continue; } //remove unwanted planes from the list if (flag == 0) { //remove unwanted faces from the list (this works only once) pFace = UtilityMethods.GetPossiblePlanes(pFace, pipeCurve.get_EndPoint(0), transform); flag = -1; } //if no plane is found for intersect to work if (pFace.Count == 0) { MessageBox.Show("Sorry! No structural element found"); return(Result.Succeeded); } //from the specification file, get the spacing corresponding to the pipe diameter spacing = UtilityMethods.GetSpacing(ele, specList); //check if the spacing returned is -1 if (spacing == -1) { FailedToPlace.Add(ele.Id); continue; } //get the points for placing the family instances points = UtilityMethods.GetPlacementPoints(spacing, pipeCurve, 1000 * configFileData.offset, 1000 * configFileData.minSpacing); //check if the points is null exception if (points == null) { FailedToPlace.Add(ele.Id); continue; } //get the pipe level pipeLevel = ele.Level; //iterate through all the points for placing the family instances foreach (XYZ point in points) { try { //create the instances at each points tempEle = m_document.Document.Create.NewFamilyInstance (point, symbol, ele, pipeLevel, StructuralType.NonStructural); createdElements.Add(tempEle.Id); } catch { FailedToPlace.Add(ele.Id); continue; } //find the rod length required rodLength = UtilityMethods.ReturnLeastZ_Value(m_document.Document, pFace, point, transform); if (rodLength == -1) { FailedToPlace.Add(ele.Id); createdElements.Remove(tempEle.Id); m_document.Document.Delete(tempEle.Id); continue; } //adjust the newly created element properties based on the rodlength, //orientation and dia of pipe if (!UtilityMethods.AdjustElement(m_document.Document, tempEle, point, (Pipe)ele, rodLength, pipeCurve)) { FailedToPlace.Add(ele.Id); createdElements.Remove(tempEle.Id); m_document.Document.Delete(tempEle.Id); continue; } } } } return(Result.Succeeded); } catch (Exception e) { message = e.Message; return(Autodesk.Revit.UI.Result.Failed); } throw new NotImplementedException(); }