public void BeWorkset(string _wsName, ExternalCommandData commandData) { UIApplication _uiapp = commandData.Application; UIDocument _uidoc = _uiapp.ActiveUIDocument; Autodesk.Revit.ApplicationServices.Application _app = _uiapp.Application; Autodesk.Revit.DB.Document _doc = _uidoc.Document; WorksetTable wst = _doc.GetWorksetTable(); WorksetId wsID = FamilyUtils.WhatIsThisWorkSetIDByName(_wsName, _doc); if (wsID != null) { using (Transaction trans = new Transaction(_doc, "WillChangeWorkset")) { trans.Start(); wst.SetActiveWorksetId(wsID); trans.Commit(); } } else { System.Windows.MessageBox.Show("Sorry but there is no workset " + _wsName + " to switch to.", "Smells So Bad It Has A Chain On It", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation); } }
private void CheckThisFamilyPairing(Autodesk.Revit.DB.Document doc, Type targetType, string familyTagName, string familyTagSymbName, BuiltInCategory bic) { Element ConfirmTag = FamilyUtils.FindFamilyType(doc, targetType, familyTagName, familyTagSymbName, bic); if (ConfirmTag == null) { FamilyUtils.SayMsg("Road Closed", "Unable to resolve loading family " + familyTagName + " that has a type " + familyTagSymbName + "Maybe the tool settings are not correct."); } }
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) { Autodesk.Revit.DB.Document doc = commandData.Application.ActiveUIDocument.Document; PlunkOClass plunkThis = new PlunkOClass(commandData.Application); string wsName = "ELEC LIGHTING DIAGNOSTIC"; string FamilyName = "OCC-SENSOR-DETECTION-CMR09"; string FamilySymbolName = "DET BLADES ALL"; string pNameOccHeight = "HEIGHT_ABOVE_FLOOR"; bool oneShot = true; BuiltInCategory bicFamily = BuiltInCategory.OST_LightingDevices; if (plunkThis.NotInThisView()) { return(Result.Succeeded); } CheckThisFamilyPairing(doc, typeof(FamilySymbol), FamilyName, FamilySymbolName, bicFamily); Element elemPlunked = null; double optOffset = plunkThis.GetCeilingHeight("OCC Sensor Tool Plunk"); Double pOffSetX = 0.0; Double pOffSetY = 0.0; Double pOffSetZ = 0.0 + optOffset; Units unit = commandData.Application.ActiveUIDocument.Document.GetUnits(); string optMSG = " : will be at " + UnitFormatUtils.Format(unit, UnitType.UT_Length, optOffset, false, false); if (optOffset != 0.0) { plunkThis.PlunkThisFamilyType(FamilyName, FamilySymbolName, wsName, bicFamily, out elemPlunked, oneShot, pOffSetX, pOffSetY, pOffSetZ, optMSG); } /// At this point there may or may not have been an element placed. #region SetParametersSection if (elemPlunked != null) { using (Transaction tp = new Transaction(doc, "PlunkOMatic:SetParam")) { tp.Start(); //TaskDialog.Show(_pName, _pName); Parameter parToSet = null; parToSet = elemPlunked.LookupParameter(pNameOccHeight); string strVal = UnitFormatUtils.Format(unit, UnitType.UT_Length, optOffset, false, false); if (null != parToSet) { parToSet.SetValueString(strVal); // this parameter is distance, therefore valuestring } else { FamilyUtils.SayMsg("Cannot Set Parameter Value: " + strVal, "... because parameter:\n" + pNameOccHeight + "\ndoes not exist in the family:\n" + FamilyName + "\nof Category:\n" + bicFamily.ToString().Replace("OST_", "")); } tp.Commit(); } } #endregion return(Result.Succeeded); }
private void InitSelectorCombo(bool scanDocDB) { pfTagNamePair.Items.Clear(); switch (CurContextMode) { case LTMode: if (scanDocDB) { // Dictionary of Family Tagmanes and their symbols (i.e. types) Dictionary <string, List <FamilySymbol> > lightFixTagTypes = FamilyUtils.FindFamilyTypes(_doc, BuiltInCategory.OST_LightingFixtureTags); // Now make a pairings list tagNamTypeLstLT.Clear(); foreach (KeyValuePair <string, List <FamilySymbol> > entry in lightFixTagTypes) { string tagName = entry.Key; foreach (FamilySymbol item in entry.Value) { string tagNameTypePairing = tagName + delim + item.Name; tagNamTypeLstLT.Add(tagNameTypePairing); } } } // Rebuild pairings list to only those entries that satisfy filter strings if (curQuickFltrStr.Length > 0) { //List<string> qFltrList = tagNamTypeLst.FindAll(t => t.ToLower().Contains(_qFltr)); // lists only that satisfy containing every filter item separated by a comma string fltrStr = tboxQfltr.Text; char chrDelim = ','; // DO NOT CHANGE string[] words = fltrStr.Split(chrDelim); List <string> keywords = words.Select(x => x.Trim()).ToList(); List <string> qFltrList = tagNamTypeLstLT.FindAll(t => keywords.All(t.ToLower().Contains)); tagNamTypeLstLT = qFltrList; } // Fill combobox using list in sorted form and stripping off OST_. try { foreach (string itm in tagNamTypeLstLT.OrderBy(i => i)) { if (itm != "INVALID") { pfTagNamePair.Items.Add(itm.Replace("OST_", "")); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Problem at InitSelectorCombo()"); } pfTagNamePair.SelectedIndex = 0; if (pfFamLTTagPair != null) { if (pfTagNamePair.Items.Contains(pfFamLTTagPair)) { pfTagNamePair.Text = pfFamLTTagPair; } } MsgTextBlockMainMsg.Text = "Set Tag | Type" + " (" + tagNamTypeLstLT.Count.ToString() + ")"; break; case SWMode: if (scanDocDB) { // Dictionary of Family Tagmanes and their symbols (i.e. types) Dictionary <string, List <FamilySymbol> > switchTagTypes = FamilyUtils.FindFamilyTypes(_doc, BuiltInCategory.OST_LightingDeviceTags); // Now make a pairings list tagNamTypeLstSW.Clear(); foreach (KeyValuePair <string, List <FamilySymbol> > entry in switchTagTypes) { string tagName = entry.Key; foreach (FamilySymbol item in entry.Value) { string tagNameTypePairing = tagName + delim + item.Name; tagNamTypeLstSW.Add(tagNameTypePairing); } } } // Rebuild pairings list to only those entries that satisfy filter strings if (curQuickFltrStr.Length > 0) { //List<string> qFltrList = tagNamTypeLst.FindAll(t => t.ToLower().Contains(_qFltr)); // lists only that satisfy containing every filter item separated by a comma string fltrStr = tboxQfltr.Text; char chrDelim = ','; // DO NOT CHANGE string[] words = fltrStr.Split(chrDelim); List <string> keywords = words.Select(x => x.Trim()).ToList(); List <string> qFltrList = tagNamTypeLstSW.FindAll(t => keywords.All(t.ToLower().Contains)); tagNamTypeLstSW = qFltrList; } // Fill combobox using list in sorted form and stripping off OST_. try { foreach (string itm in tagNamTypeLstSW.OrderBy(i => i)) { if (itm != "INVALID") { pfTagNamePair.Items.Add(itm.Replace("OST_", "")); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Problem at InitSelectorCombo()"); } pfTagNamePair.SelectedIndex = 0; if (pfFamSWTagPair != null) { if (pfFamSWTagPair != "") { if (pfTagNamePair.Items.Contains(pfFamSWTagPair)) { pfTagNamePair.Text = pfFamSWTagPair; } } } MsgTextBlockMainMsg.Text = "Set Tag | Type" + " (" + tagNamTypeLstSW.Count.ToString() + ")"; break; default: break; } }
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); }