Write end of Sheet Number to Shared Parameter
This Revit macro will take the sheet number from the "SHEET_NUMBER" built-in parameter, and write the last 6 digits of that sheet number to a shared instance parameter in the title block.
public void SheetParameter()
{
//standard setup lines - uidoc gives access to the revit user interface, and doc gives access to the open revit file
UIDocument uidoc = this.ActiveUIDocument;
Document doc = this.ActiveUIDocument.Document;
//gets the current view
Autodesk.Revit.DB.View pView = ActiveUIDocument.Document.ActiveView;
//gets selected objects
Selection selection = uidoc.Selection;
ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
//set up some objects or variables to store
//seting them up here gives access throughout this macro
IList<Element> ElementsOnSheet = new List<Element>();
Element MyTitleBlock = null;
string message = "";
string SubString = "";
Parameter p = null;
IList<Element> m_alltitleblocks = new List<Element>();
// get all the sheets in the current revit project (if more than one project is open, it will use the project in the active view.)
List<ViewSheet> sheets = new FilteredElementCollector(doc).WhereElementIsNotElementType() .OfClass(typeof(ViewSheet)).Cast<ViewSheet>().ToList();
//now we need to set up a transaction before anyting in the revit file is changed
using (Transaction trans = new Transaction(doc, "Update sheet numbers"))
{
trans.Start();
//go through each sheet in the revit project
foreach( ViewSheet vs in sheets)
{
//we need a try statement to avoid crashing this macro if the sheet names are less than 6 digits
try
{
//get the sheet number parameter, the sheet number, and teh last 6 digits
p = vs.get_Parameter(BuiltInParameter.SHEET_NUMBER);
string mystring = p.AsString();
SubString = mystring.Substring(mystring.Length-6);
//find all the elements on the sheet - one should be the title block
foreach (Element e in new FilteredElementCollector(doc).OwnedByView(vs.Id))
{
ElementsOnSheet.Add(e);
}
//get all the title blocks in the project
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(FamilySymbol));
collector.OfCategory(BuiltInCategory.OST_TitleBlocks);
m_alltitleblocks = collector.ToElements();
//go through all the elements on the sheet and see if one has an ID that matches a title block
foreach (Element el in ElementsOnSheet)
{
foreach (FamilySymbol Fs in m_alltitleblocks)
{
if (el.GetTypeId().IntegerValue == Fs.Id.IntegerValue)
{
//when a match is found, set it equal to MyTitleBlock
//this next line was a problem - I was picking the family definition, not
//the title block on the sheet
//MyTitleBlock = Fs;
//here is what is needed
MyTitleBlock = el;
} // end if
} // end foreach familysymbol
}//end foreach element
//now that we have the titleblock, look for the parameter
Parameter myParam = MyTitleBlock.get_Parameter(new Guid ( "75b07d4f-f37a-4658-9ce6-45d8f20c10f2"));
//testing task dialogs
//TaskDialog.Show("Titleblock Name", MyTitleBlock.Name.ToString ());
//TaskDialog.Show("Parameter Name", myParam.Definition.Name.ToString());
//TaskDialog.Show("Param Value AsString", myParam.AsString ());
//depending on the paramter, sometimes asstring works, sometimes we need asvaluestring, and sometimes we need tostring
//trial and error to see what works is the only way I can figure it out
//TaskDialog.Show("Param Value As Value String", myParam.AsValueString ());
//TaskDialog.Show("Param Value To String", myParam.ToString ());
//TaskDialog.Show("Sheet Index", SubString);
//here is where the parameter is actually set to the last 6 digits of teh sheet number
bool b = myParam.Set (SubString);
//another troubleshooting dialog
TaskDialog.Show("Set Param Sucess?", "Sheet #: " + mystring + " " + b.ToString());
}// end try
catch
{
}
//send a messagebox so the user knows what sheets were found
message = message + p.AsString() + " " + SubString + "\n\r";
}// end foreach sheet
trans.Commit ();
} //end using transaction
TaskDialog.Show("Revit - all the sheet numbers are", message);
} //end public void SheetParameter