So I'm attempting to edit the Type Comments for all Framing objects currently in a model. When I attempt to display the information gathered, everything works beautifully. However, I'm stuck on how to make the actual change to the Type Comments field... Any thoughts? (Be gentle, I'm just wrapping my head around C#)
using System;
using System.Reflection;
using System.Windows.Media.Imaging;
using System.Collections;
using System.Collections.Generic;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
namespace _TT_Revit_Tools
{
public class ttAddPanel : IExternalApplication
{
public Result OnStartup(UIControlledApplication application)
{
RibbonPanel ribbonPanel = application.CreateRibbonPanel("TT Revit Tools");
string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
PushButtonData buttonData = new PushButtonData("cmdDocument_Selection",
"Framing Type Comments",thisAssemblyPath,"_TT_Revit_Tools.Document_Selection");
PushButton pushbutton = ribbonPanel.AddItem(buttonData) as PushButton;
pushbutton.ToolTip = "Get Element ID of selected objects.";
return Result.Succeeded;
}
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
}
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.ReadOnly)]
public class Document_Selection : IExternalCommand
{
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
ref string message, ElementSet elements)
{
try
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document document = uidoc.Document;
ElementClassFilter familyInstanceFilter = new ElementClassFilter(typeof(FamilyInstance));
ElementCategoryFilter framingCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming);
LogicalAndFilter framingInstanceFilter = new LogicalAndFilter(familyInstanceFilter, framingCategoryFilter);
FilteredElementCollector collector = new FilteredElementCollector(document);
ICollection<ElementId> framing = collector.WherePasses(framingInstanceFilter).ToElementIds();
foreach (ElementId id in framing)
{
//Obtain Shape
string name = document.GetElement(id).Name;
string shape = name.Substring(0, name.IndexOf("X"));
//get element
Element e = document.GetElement(id);
//get element type ID
ElementId typeID = e.GetTypeId();
//get element type
Element framingType = document.GetElement(typeID);
//get type comment
Parameter typeComments = framingType.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_COMMENTS);
typeComments.Set(shape);
}
}
catch (Exception ex)
{
message = ex.Message;
return Autodesk.Revit.UI.Result.Failed;
}
return Autodesk.Revit.UI.Result.Succeeded;
}
}
}
Solved! Go to Solution.
So I'm attempting to edit the Type Comments for all Framing objects currently in a model. When I attempt to display the information gathered, everything works beautifully. However, I'm stuck on how to make the actual change to the Type Comments field... Any thoughts? (Be gentle, I'm just wrapping my head around C#)
using System;
using System.Reflection;
using System.Windows.Media.Imaging;
using System.Collections;
using System.Collections.Generic;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
namespace _TT_Revit_Tools
{
public class ttAddPanel : IExternalApplication
{
public Result OnStartup(UIControlledApplication application)
{
RibbonPanel ribbonPanel = application.CreateRibbonPanel("TT Revit Tools");
string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
PushButtonData buttonData = new PushButtonData("cmdDocument_Selection",
"Framing Type Comments",thisAssemblyPath,"_TT_Revit_Tools.Document_Selection");
PushButton pushbutton = ribbonPanel.AddItem(buttonData) as PushButton;
pushbutton.ToolTip = "Get Element ID of selected objects.";
return Result.Succeeded;
}
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
}
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.ReadOnly)]
public class Document_Selection : IExternalCommand
{
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
ref string message, ElementSet elements)
{
try
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document document = uidoc.Document;
ElementClassFilter familyInstanceFilter = new ElementClassFilter(typeof(FamilyInstance));
ElementCategoryFilter framingCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming);
LogicalAndFilter framingInstanceFilter = new LogicalAndFilter(familyInstanceFilter, framingCategoryFilter);
FilteredElementCollector collector = new FilteredElementCollector(document);
ICollection<ElementId> framing = collector.WherePasses(framingInstanceFilter).ToElementIds();
foreach (ElementId id in framing)
{
//Obtain Shape
string name = document.GetElement(id).Name;
string shape = name.Substring(0, name.IndexOf("X"));
//get element
Element e = document.GetElement(id);
//get element type ID
ElementId typeID = e.GetTypeId();
//get element type
Element framingType = document.GetElement(typeID);
//get type comment
Parameter typeComments = framingType.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_COMMENTS);
typeComments.Set(shape);
}
}
catch (Exception ex)
{
message = ex.Message;
return Autodesk.Revit.UI.Result.Failed;
}
return Autodesk.Revit.UI.Result.Succeeded;
}
}
}
Solved! Go to Solution.
Solved by Troy_Gates. Go to Solution.
Since you are changing the model by editing the parameters, you need to use a transaction.
Since you are changing the model by editing the parameters, you need to use a transaction.
Thank you Troy, you were correct. Adding the transaction resolved the issue.
using (Transaction transaction = new Transaction(document, "Type Comments"))
{
transaction.Start();
if (typeComments.ToString() != shape)
{
if (shape != "")
{
FamilyInstance famInstance = e as FamilyInstance;
famInstance.Symbol.get_Parameter("Type Comments").Set(shape);
//TaskDialog.Show("Revit", "Completed - set " + id.IntegerValue + " to " + shape);
}
}
transaction.Commit();
}
Thank you Troy, you were correct. Adding the transaction resolved the issue.
using (Transaction transaction = new Transaction(document, "Type Comments"))
{
transaction.Start();
if (typeComments.ToString() != shape)
{
if (shape != "")
{
FamilyInstance famInstance = e as FamilyInstance;
famInstance.Symbol.get_Parameter("Type Comments").Set(shape);
//TaskDialog.Show("Revit", "Completed - set " + id.IntegerValue + " to " + shape);
}
}
transaction.Commit();
}
Mkrebs,
As far as I know, to be able to write value into "Type Comments" parameter, you need to change your current transaction mode as follow:
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
Mkrebs,
As far as I know, to be able to write value into "Type Comments" parameter, you need to change your current transaction mode as follow:
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
Can't find what you're looking for? Ask the community or share your knowledge.