Guest User

Untitled

a guest
Aug 19th, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. using Autodesk.Revit.Attributes;
  2. using Autodesk.Revit.DB;
  3. using Autodesk.Revit.UI;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7.  
  8. [Transaction(TransactionMode.Manual)]
  9. public class SetCustomScaleParameterForSheets : IExternalCommand
  10. {
  11. public Result Execute(
  12. ExternalCommandData commandData,
  13. ref string message,
  14. ElementSet elements)
  15. {
  16. UIDocument uidoc = commandData.Application.ActiveUIDocument;
  17. Document doc = uidoc.Document;
  18.  
  19. // Get all global and shared parameters from the entire project
  20. List<BindingMap> bindingMaps = new List<BindingMap>
  21. {
  22. doc.ParameterBindings
  23. };
  24.  
  25. HashSet<string> globalAndSharedParameterNames = new HashSet<string>();
  26.  
  27. foreach (BindingMap bindingMap in bindingMaps)
  28. {
  29. DefinitionBindingMapIterator iter = bindingMap.ForwardIterator();
  30. iter.Reset();
  31. while (iter.MoveNext())
  32. {
  33. Definition def = iter.Key as Definition;
  34. if (def != null)
  35. {
  36. // Add the parameter name to the set
  37. globalAndSharedParameterNames.Add(def.Name);
  38. }
  39. }
  40. }
  41.  
  42. // Convert global and shared parameters to a list of dictionaries for display
  43. List<Dictionary<string, object>> parameterEntries = globalAndSharedParameterNames
  44. .Select(paramName => new Dictionary<string, object>
  45. {
  46. { "Parameter Name", paramName }
  47. }).ToList();
  48.  
  49. // Show parameters selection dialog
  50. List<string> parameterProperties = new List<string> { "Parameter Name" };
  51. List<Dictionary<string, object>> selectedParameter = CustomGUIs.DataGrid(parameterEntries, parameterProperties, false);
  52.  
  53. if (selectedParameter == null || selectedParameter.Count == 0)
  54. {
  55. return Result.Cancelled;
  56. }
  57.  
  58. // Get all sheets in the project and store both Title and ElementId
  59. FilteredElementCollector collector = new FilteredElementCollector(doc);
  60. ICollection<Element> allSheets = collector.OfClass(typeof(ViewSheet)).ToElements();
  61.  
  62. List<Dictionary<string, object>> sheetEntries = allSheets
  63. .Select(sheet => new Dictionary<string, object>
  64. {
  65. { "Title", $"{(sheet as ViewSheet).SheetNumber} - {sheet.Name}" },
  66. { "ElementId", sheet.Id } // Store the actual ElementId
  67. }).ToList();
  68.  
  69. // Show sheets selection dialog
  70. List<string> sheetProperties = new List<string> { "Title", "ElementId" };
  71. List<Dictionary<string, object>> selectedSheets = CustomGUIs.DataGrid(sheetEntries, sheetProperties, false);
  72.  
  73. if (selectedSheets == null || selectedSheets.Count == 0)
  74. {
  75. return Result.Cancelled;
  76. }
  77.  
  78. string parameterName = selectedParameter[0]["Parameter Name"].ToString();
  79. List<string> inconsistentSheets = new List<string>();
  80.  
  81. using (Transaction trans = new Transaction(doc, "Set Custom Scale Parameter for Sheets"))
  82. {
  83. trans.Start();
  84.  
  85. foreach (var sheetDict in selectedSheets)
  86. {
  87. if (sheetDict.TryGetValue("ElementId", out object elementIdObj) && elementIdObj is ElementId elementId)
  88. {
  89. ViewSheet sheet = doc.GetElement(elementId) as ViewSheet;
  90.  
  91. if (sheet != null)
  92. {
  93. // Get the scales of the views on the sheet
  94. var viewports = new FilteredElementCollector(doc)
  95. .OfClass(typeof(Viewport))
  96. .WhereElementIsNotElementType()
  97. .Cast<Viewport>()
  98. .Where(vp => vp.SheetId == sheet.Id);
  99.  
  100. var viewScales = viewports
  101. .Select(vp => doc.GetElement(vp.ViewId) as View)
  102. .Where(v => v != null && v.ViewType != ViewType.Legend) // Exclude legends
  103. .Select(v => v.Scale)
  104. .Distinct()
  105. .ToList();
  106.  
  107. if (viewScales.Count == 1) // All views have the same scale
  108. {
  109. string scaleValue = $"1:{viewScales.First()}";
  110. Parameter param = sheet.LookupParameter(parameterName);
  111. if (param != null && param.StorageType == StorageType.String)
  112. {
  113. param.Set(scaleValue);
  114. }
  115. }
  116. else
  117. {
  118. // Log the sheet as having inconsistent scales
  119. inconsistentSheets.Add($"{sheet.SheetNumber} - {sheet.Name}");
  120. }
  121. }
  122. }
  123. else
  124. {
  125. message = "Error: Unable to find the ElementId key in the selected sheet dictionary.";
  126. return Result.Failed;
  127. }
  128. }
  129.  
  130. trans.Commit();
  131. }
  132.  
  133. if (inconsistentSheets.Count > 0)
  134. {
  135. // Display a message showing the sheets with inconsistent scales
  136. TaskDialog.Show("Inconsistent Scales",
  137. "The following sheets have views with different scales and were not updated:\n" +
  138. string.Join("\n", inconsistentSheets));
  139. }
  140.  
  141. return Result.Succeeded;
  142. }
  143. }
  144.  
Advertisement
Add Comment
Please, Sign In to add comment