@jeremytammik
Good morning,
i writed this code to set the numbering automatically, but in each time I re-execute my code it begin the numbering from 0 instead of lastassignednumber.
so where is the error ?
Thanks in advance,
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.DB.ExtensibleStorage;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Number
{
[Transaction(TransactionMode.Manual)]
public class Code : IExternalCommand
{
private const string SchemaGUID = "633FEF1C-4DF3-4C07-A812-D392C8015194";
private const string MinRangePropertyName = "MinRange";
private const string MaxRangePropertyName = "MaxRange";
private const string LastAssignedNumberPropertyName = "LastAssignedNumber";
private int currentNumber;
private int minRange;
private int maxRange;
private ICollection<ElementId> addedElementIds = new List<ElementId>();
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
Document doc = uiDoc.Document;
addedElementIds = GetWallElement(doc);
if (!RetrieveRangeValues(doc))
{
// Range values not found, prompt the user to enter the range
using (Window window = new Window(doc))
{
window.ShowDialog();
minRange = window.minRange;
maxRange = window.maxRange;
currentNumber = minRange;
StoreRangeValues(doc);
}
}
else
{
currentNumber = RetrieveLastAssignedNumber(doc);
if (currentNumber < minRange || currentNumber > maxRange)
{
// Handle the case where the last assigned number is outside the specified range
// You can display an error message or take appropriate action here
}
TaskDialog.Show("Range", "Your entered range is between: \n" + minRange.ToString() + " - " + maxRange.ToString() + "\n" +
"Current Number: " + currentNumber);
}
// Subscribe to the Idling event
uiApp.Idling += HandleIdlingEvent;
return Result.Succeeded;
}
private void HandleIdlingEvent(object sender, IdlingEventArgs e)
{
UIApplication uiApp = sender as UIApplication;
Document doc = uiApp.ActiveUIDocument.Document;
try
{
// Check for newly added walls in the document
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfCategory(BuiltInCategory.OST_Walls);
ICollection<ElementId> currentElementIds = collector.ToElementIds();
// Find the added wall element ids by comparing the current element ids with the previously stored ids
IEnumerable<ElementId> newElementIds = currentElementIds.Except(addedElementIds);
if (newElementIds.Any())
{
using (Transaction trans = new Transaction(doc, "Assign Number to Walls"))
{
trans.Start();
foreach (ElementId elementId in newElementIds)
{
Element element = doc.GetElement(elementId);
if (element is Wall wall)
{
// Get the custom "Number" parameter
Parameter parameter = wall.LookupParameter("Number");
if (parameter != null && parameter.StorageType == StorageType.String)
{
parameter.Set(currentNumber.ToString());
currentNumber++;
}
}
}
trans.Commit();
// Update the added element ids for the next idling event
addedElementIds = currentElementIds;
// Store the last assigned number
StoreLastAssignedNumber(doc, currentNumber);
}
}
}
catch (Exception ex)
{
TaskDialog.Show("Error", ex.Message);
}
}
public ICollection<ElementId> GetWallElement(Document doc)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfCategory(BuiltInCategory.OST_Walls);
return collector.ToElementIds().ToList();
}
private bool RetrieveRangeValues(Document doc)
{
try
{
Schema schema = Schema.Lookup(new Guid(SchemaGUID));
if (schema != null)
{
Entity entity = doc.ProjectInformation.GetEntity(schema);
if (entity.IsValid())
{
minRange = entity.Get<int>(schema.GetField(MinRangePropertyName));
maxRange = entity.Get<int>(schema.GetField(MaxRangePropertyName));
currentNumber = entity.Get<int>(schema.GetField(LastAssignedNumberPropertyName));
currentNumber = RetrieveLastAssignedNumber(doc); // Retrieve the last assigned number
return true;
}
}
}
catch (Exception ex)
{
TaskDialog.Show("Error", ex.Message);
}
return false;
}
private int RetrieveLastAssignedNumber(Document doc)
{
try
{
Schema schema = Schema.Lookup(new Guid(SchemaGUID));
if (schema != null)
{
Entity entity = doc.ProjectInformation.GetEntity(schema);
if (entity.IsValid())
{
return entity.Get<int>(schema.GetField(LastAssignedNumberPropertyName));
}
}
}
catch (Exception ex)
{
TaskDialog.Show("Error", ex.Message);
}
// Return a default value if the last assigned number cannot be retrieved
return 0;
}
private void StoreRangeValues(Document doc)
{
try
{
Schema schema = Schema.Lookup(new Guid(SchemaGUID));
if (schema == null)
{
SchemaBuilder schemaBuilder = new SchemaBuilder(new Guid(SchemaGUID));
schemaBuilder.SetSchemaName("NumberSchema");
schemaBuilder.SetReadAccessLevel(AccessLevel.Public);
schemaBuilder.SetWriteAccessLevel(AccessLevel.Public);
FieldBuilder minRangeField = schemaBuilder.AddSimpleField(MinRangePropertyName, typeof(int));
FieldBuilder maxRangeField = schemaBuilder.AddSimpleField(MaxRangePropertyName, typeof(int));
FieldBuilder lastAssignedNumberField = schemaBuilder.AddSimpleField(LastAssignedNumberPropertyName, typeof(int));
schema = schemaBuilder.Finish();
}
using (Transaction trans = new Transaction(doc, "Store Range Values"))
{
trans.Start();
Entity entity = new Entity(schema);
entity.Set<int>(schema.GetField(MinRangePropertyName), minRange);
entity.Set<int>(schema.GetField(MaxRangePropertyName), maxRange);
doc.ProjectInformation.SetEntity(entity);
trans.Commit();
}
}
catch (Exception ex)
{
TaskDialog.Show("Error", ex.Message);
}
}
private void StoreLastAssignedNumber(Document doc, int number)
{
try
{
Schema schema = Schema.Lookup(new Guid(SchemaGUID));
if (schema != null)
{
Entity entity = doc.ProjectInformation.GetEntity(schema);
if (entity.IsValid())
{
entity.Set<int>(schema.GetField(LastAssignedNumberPropertyName), number);
}
else
{
entity = new Entity(schema);
entity.Set<int>(schema.GetField(LastAssignedNumberPropertyName), number);
doc.ProjectInformation.SetEntity(entity);
}
}
}
catch (Exception ex)
{
TaskDialog.Show("Error", ex.Message);
}
}
}
}