public void StartLightFinding(UIApplication m_app) { UIDocument uidoc = m_app.ActiveUIDocument; Document doc = uidoc.Document; string ThisProjectFile = System.IO.Path.GetFileNameWithoutExtension(doc.PathName); string rootExportFolder = "c:\\temp\\"; string outPutTail = "_LPD.csv"; string exportToCVSFileName; string pNamePWR = "INPUT POWER"; string strHead; IntPtr revitHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; bool detailRpt = true; // phase basis is the phase on which element status is determined for inclusion Phase phsBasis = FamilyUtils.GetDesiredPhaseBasis(doc); if (!Directory.Exists(rootExportFolder)) { try { Directory.CreateDirectory(rootExportFolder); } catch (Exception) { TaskDialog.Show("Cannot create the output folder " + rootExportFolder, "Exiting"); return; } } TaskDialog td = new TaskDialog("Find Lights"); td.MainIcon = TaskDialogIcon.TaskDialogIconNone; td.Title = "Writing Lighting Information To CSV File"; td.TitleAutoPrefix = false; td.AllowCancellation = true; td.MainInstruction = "Select either verbose or brief output. Verbose includes a room by room fixture breakdown. Brief provides only the power totals."; td.MainContent = "The files will be CSV format written to " + rootExportFolder + ". They will be timestamp named and opened for you, presumably by Excel."; td.CommonButtons = TaskDialogCommonButtons.Cancel; td.DefaultButton = TaskDialogResult.Cancel; td.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Brief Report"); td.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "Verbose Report"); TaskDialogResult tdRes = td.Show(); if (tdRes == TaskDialogResult.Cancel) { return; } if (TaskDialogResult.CommandLink1 == tdRes) // "Brief Report" { detailRpt = false; } if (TaskDialogResult.CommandLink2 == tdRes) // "Verbose Report" { detailRpt = true; } if (detailRpt) { strHead = "Room Number,Room Name,Floor Level,Area SF,Item Name,Item Count,Item Watts,Total Watts,Pwr Density Contribution"; exportToCVSFileName = rootExportFolder + ThisProjectFile + "_VERBOSE" + outPutTail; } else { strHead = "Room Number,Room Name,Area SF,L.Pwr. Density w/sf,Tot. W"; exportToCVSFileName = rootExportFolder + ThisProjectFile + "_BRIEF" + outPutTail; } exportToCVSFileName = AssignNewUniqueFileName(exportToCVSFileName); //System.Windows.MessageBox.Show("Will write to: " + exportToCVSFileName, "FYI"); FormMsgWPF waitItOut = new FormMsgWPF(); waitItOut.SetMsg("Trust this, just wait it out if Revit goes 'Not Responding'.", "Believe It Or Not"); waitItOut.Show(); using (StreamWriter writer = new StreamWriter(exportToCVSFileName)) { writer.WriteLine("Data is for items existing and not demolished in phases up to and including: " + phsBasis.Name); writer.WriteLine(strHead); try { // Make sure no other RVTs are open in Revit if (MultipleRVTsOpen(m_app) == true) { TaskDialog.Show("Process Stopped", "Please only have one file open when running this tool"); return; } // Iterate through each document foreach (Document _doc in m_app.Application.Documents) { // Only process links if (_doc.IsLinked) { #region Create a room collection from rooms in the current link RoomFilter roomFilter = new RoomFilter(); FilteredElementCollector filteredElemCol = new FilteredElementCollector(_doc); filteredElemCol.WherePasses(roomFilter).WhereElementIsNotElementType(); #endregion string strRoomData = ""; // Changed to this go get to work in Revit 2015 int eleCnt = filteredElemCol.ToList().Count(); // Originally was this, working in Revit 2016 // int eleCnt = filteredElemCol.GetElementCount(); #region ProcessCollection if any if (eleCnt > 0) { string strPurpose = "Rooms In " + System.IO.Path.GetFileNameWithoutExtension(_doc.PathName); SetStatusBarText(revitHandle, "Starting scan ..." + strPurpose); #region Iterate through each room foreach (Room _room in filteredElemCol) { // Only process placed rooms if (IsRoomPlaced(_room) == false) { continue; } double selRmArea = _room.Area; // Get all LightingFixtures in the current room BuiltInCategory bic = BuiltInCategory.OST_LightingFixtures; List <FamilyInstance> famInstances = FamilyUtils.FamilyInstanceCategoryInThisRoom(_room, bic, m_app.ActiveUIDocument.Document, phsBasis); //System.Windows.MessageBox.Show("Have list for "+ _room.Name); if (famInstances != null) { Dictionary <string, int> dicFamInstances = new Dictionary <string, int>(); Dictionary <string, double> dicLightFixTypeIP = new Dictionary <string, double>(); if (famInstances != null) { foreach (FamilyInstance fi in famInstances) { string fiNamePair = fi.Symbol.FamilyName + " | " + fi.Symbol.Name; //msg = msg + "\n" + fiNamePair; int qty; if (dicFamInstances.TryGetValue(fiNamePair, out qty)) { dicFamInstances[fiNamePair] = qty + 1; } else { dicFamInstances.Add(fiNamePair, 1); Parameter pPwr = fi.Symbol.LookupParameter(pNamePWR); if (pPwr != null) { double convVal = FamilyUtils.ConvertParmValueFromRaw(pPwr.AsDouble()); dicLightFixTypeIP.Add(fiNamePair, convVal); } } } } string msgDetail = ""; double totRoomLWatts = 0.0; foreach (var item in dicFamInstances) { string itemName = item.Key; int itemCount = item.Value; double itemWatts; if (dicLightFixTypeIP.TryGetValue(item.Key, out itemWatts)) { double totalWattsForItem = itemWatts * Convert.ToDouble(itemCount); double pwrDensContribution = totalWattsForItem / _room.Area; totRoomLWatts = totRoomLWatts + totalWattsForItem; msgDetail = msgDetail + "\n" + itemName + " cnt " + itemCount.ToString() + " @ " + itemWatts.ToString("0.00") + " for " + totalWattsForItem.ToString("0.0 w"); if (detailRpt) { msgDetail = _room.Number + "," + _room.Name + "," + _room.Level.Name + "," + _room.Area.ToString("0.00") + "," + itemName + "," + itemCount.ToString() + "," + itemWatts.ToString("0.00") + "," + totalWattsForItem.ToString("0.00") + "," + pwrDensContribution.ToString("0.00000000"); writer.WriteLine(msgDetail); } } else { /// item key not in dictionary! } } double lightFixPWRDensity = totRoomLWatts / selRmArea; string msgMain = "Rm Area: " + selRmArea.ToString("0.00 sf") + " L Pwr. Density: " + lightFixPWRDensity.ToString("0.00 w/sf") + "\n" + msgDetail; string strLine = _room.Number + "," + _room.Name + "," + selRmArea.ToString("0.00") + "," + lightFixPWRDensity.ToString("0.00") + "," + totRoomLWatts.ToString(); string strProgressWPF = "Room: " + _room.Number + " Name: " + _room.Name + " sf:" + selRmArea.ToString("0.00") + " w/sf:" + lightFixPWRDensity.ToString("0.00"); strProgressWPF = strProgressWPF + " ... wait it out if Revit goes Not Responding"; SetStatusBarText(revitHandle, strProgressWPF); strRoomData = strRoomData + Environment.NewLine + strLine; if (!detailRpt) { writer.WriteLine(strLine); } } // end if family instances } // end for each room #endregion string msgFullBody = strHead + strRoomData; #region Debug Show Results //string msgFullBody = strHead + strRoomData; //TaskDialog thisDialog = new TaskDialog(System.IO.Path.GetFileNameWithoutExtension(_doc.PathName)); //thisDialog.TitleAutoPrefix = false; //thisDialog.MainIcon = TaskDialogIcon.TaskDialogIconNone; //thisDialog.CommonButtons = TaskDialogCommonButtons.Close; //thisDialog.MainContent = ""; //thisDialog.MainInstruction = msgFullBody; //TaskDialogResult tResult = thisDialog.Show(); #endregion } #endregion SetStatusBarText(revitHandle, "Did do that one, whatever it was."); } // end if linked } // end foreach document SetStatusBarText(revitHandle, "Done with all linked Revit documents."); } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message + "\n" + ex.ToString(), "Error At StartLightFinding"); } } // end using streamwriter waitItOut.Close(); // Open in what system wants to use, probably Excel FileInfo fileInfo = new FileInfo(exportToCVSFileName); if (fileInfo.Exists) { System.Diagnostics.Process.Start(exportToCVSFileName); } }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uiDoc = commandData.Application.ActiveUIDocument; Document doc = uiDoc.Document; Selection sel = uiDoc.Selection; List <ElementId> selIds = new List <ElementId>(); Room thisPickRm = null; string pNamePWR = "INPUT POWER"; PlunkOClass plunkThis = new PlunkOClass(commandData.Application); if (plunkThis.NotInThisView()) { return(Result.Succeeded); } // phase basis is the phase on which element status is determined for inclusion Phase phsBasis = FamilyUtils.GetDesiredPhaseBasis(doc); FormMsgWPF thisReport = new FormMsgWPF(); string bot = "( ESC key cancels )"; string purpose = "Room Lighting - Phase Basis: " + phsBasis.Name; bool stay = true; bool doWeSelect = false; try { while (stay) { RoomSelectionFilter cf = new RoomSelectionFilter(); Reference pickedRoomReference = sel.PickObject(ObjectType.LinkedElement, cf, "Selecting Rooms Only"); if (pickedRoomReference == null) { return(Result.Failed); } // we need to get the linked document and then get the element that was picked from the LinkedElementId RevitLinkInstance linkInstance = doc.GetElement(pickedRoomReference) as RevitLinkInstance; Document linkedDoc = linkInstance.GetLinkDocument(); Element firstRoomElement = linkedDoc.GetElement(pickedRoomReference.LinkedElementId); string selRmName = ""; string selLV = ""; string sePhsCreated = ""; // string daPhsDemo = ""; string selRmNumber = ""; double selRmArea = 0.0; switch (firstRoomElement.GetType().ToString()) { case "Autodesk.Revit.DB.Architecture.Room": thisPickRm = firstRoomElement as Room; if (thisPickRm != null) { selRmName = thisPickRm.Name.ToString(); selRmNumber = thisPickRm.Number.ToString(); Phase phCR = linkedDoc.GetElement(thisPickRm.CreatedPhaseId) as Phase; if (phCR != null) { sePhsCreated = phCR.ToString(); } Level itsLevelRm = thisPickRm.Level; if (itsLevelRm != null) { selLV = itsLevelRm.Name.ToString(); selRmArea = thisPickRm.Area; //selIds.Add(thisPickRm.Id); // does not work. A known Revit fact, cannot highlight linked elements } } break; default: break; } string msgMain = firstRoomElement.Name; msgMain = msgMain + "\n" + "Rm Numb.: " + selRmNumber + "\n" + "At level: " + selLV ; // Get all LightingFixtures in the current room BuiltInCategory bic = BuiltInCategory.OST_LightingFixtures; List <FamilyInstance> famInstances = FamilyUtils.FamilyInstanceCategoryInThisRoom(thisPickRm, bic, doc, phsBasis); Dictionary <string, int> dicFamInstances = new Dictionary <string, int>(); Dictionary <string, double> dicLightFixTypeIP = new Dictionary <string, double>(); if (famInstances != null) { if (doWeSelect) { selIds.Clear(); } foreach (FamilyInstance fi in famInstances) { string fiNamePair = fi.Symbol.FamilyName + " | " + fi.Symbol.Name; //msg = msg + "\n" + fiNamePair; int qty; if (dicFamInstances.TryGetValue(fiNamePair, out qty)) { dicFamInstances[fiNamePair] = qty + 1; } else { dicFamInstances.Add(fiNamePair, 1); Parameter pPwr = fi.Symbol.LookupParameter(pNamePWR); if (pPwr != null) { double convVal = FamilyUtils.ConvertParmValueFromRaw(pPwr.AsDouble()); dicLightFixTypeIP.Add(fiNamePair, convVal); //System.Windows.Forms.MessageBox.Show(fi.Symbol.Name + " " + convVal.ToString()); } } if (doWeSelect) { selIds.Add(fi.Id); } } } string msgDetail = ""; double totRoomLWatts = 0.0; foreach (var item in dicFamInstances) { string itemName = item.Key; int itemCount = item.Value; double itemWatts; if (dicLightFixTypeIP.TryGetValue(item.Key, out itemWatts)) { double totalWattsForItem = itemWatts * Convert.ToDouble(itemCount); totRoomLWatts = totRoomLWatts + totalWattsForItem; msgDetail = msgDetail + "\n" + itemName + " cnt " + itemCount.ToString() + " @ " + itemWatts.ToString("0.00") + " for " + totalWattsForItem.ToString("0.0 w"); } else { /// item key not in dictionary! } } double lightFixPWRDensity = totRoomLWatts / selRmArea; msgMain = msgMain + "\n" + "Rm Area: " + selRmArea.ToString("0.00 sf") + " L Pwr. Density: " + lightFixPWRDensity.ToString("0.00 w/sf") + " Tot: " + totRoomLWatts.ToString("0.00 w") + "\n" + msgDetail; thisReport.SetMsg(msgMain, purpose, bot, true); thisReport.Show(); } } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { //TaskDialog.Show("Cancelled", "User cancelled"); stay = false; thisReport.Close(); uiDoc.Selection.SetElementIds(selIds); return(Result.Cancelled); } //Catch other errors catch (Exception ex) { thisReport.Close(); FamilyUtils.SayMsg("Error At RoomLightingReporter", ex.Message); return(Result.Failed); } if (doWeSelect) { uiDoc.Selection.SetElementIds(selIds); } return(Result.Succeeded); }