Message 1 of 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hey, I'm trying to create add-in to the crate level with a specific name, in the first run in Revit it works well but the second time, it gives an error that Revit couldn't complete the external command.
I thought it was because of the unique level name, but I didn't know how can I handle it.
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
using System.Diagnostics;
using System;
using Clel.Properties;
using System.Collections.Generic;
using System.Linq;
namespace Clel
{
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var uiapp = commandData.Application;
var uidoc = uiapp.ActiveUIDocument;
var app = uiapp.Application;
var doc = uidoc.Document;
//add form
Form1 fr1 = new Form1(commandData);
fr1.ShowDialog();
//add form items
string stringlevelheighvalue = fr1.levelheigh.ToString();
double intvaluelevel = double.Parse(stringlevelheighvalue);
///////////////////////////////////////////////////////////////////
string stringlevelquantityvalue = fr1.levelquantity.ToString();
int levaluequantity = int.Parse(stringlevelquantityvalue);
string Stringdecipline = fr1.Dropitem;
string std = "";
if (Stringdecipline == "Architecture")
{
std = "ARC";
}
else if (Stringdecipline == "Structure")
{
std = "STR";
}
else if (Stringdecipline == "Mechanical")
{
std = "MEP";
}
//picklevel
FilteredElementCollector coll = new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_Levels)
.WhereElementIsNotElementType().
OfClass(typeof(Level));
//
List<Level> levels = coll.Cast<Level>().ToList();
Level highestLevel = levels.OrderByDescending(l => l.Elevation).FirstOrDefault();
string highestLevelName = highestLevel.Name;
string levelBaseName = highestLevelName.Replace("Level", "").Trim();
int existlvlname = int.Parse(levelBaseName);
//
List<double> levelElevations = new List<double>();
foreach (Level lvl in coll)
{
levelElevations.Add(lvl.Elevation);
}
double highestElevationInFeet = levelElevations.Max();
//double elevationInMeters = 12.0;
//double elevationInFeet = elevationInMeters * 3.28084;
double elevationinmeter = highestElevationInFeet;
double levelvaluemeter = intvaluelevel * 3.28084;
using (Transaction tx = new Transaction(doc))
{
tx.Start("Bouttn name");
//
for (int i = 0; i < levaluequantity; i++)
{
elevationinmeter += levelvaluemeter;
Level newlevel = Level.Create(doc, elevationinmeter);
string levelName = $"{std}_Level {i + 1 + existlvlname}";
newlevel.Name = levelName;
}
tx.Commit();
return Result.Succeeded;
}
}
}
}
I really appreciated if you help me to create my addin.
Solved! Go to Solution.