Setting the Type Comments

Setting the Type Comments

mkrebs
Participant Participant
2,986 Views
3 Replies
Message 1 of 4

Setting the Type Comments

mkrebs
Participant
Participant

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;
}
}
}

0 Likes
Accepted solutions (1)
2,987 Views
3 Replies
Replies (3)
Message 2 of 4

Troy_Gates
Advocate
Advocate
Accepted solution

Since you are changing the model by editing the parameters, you need to use a transaction.

0 Likes
Message 3 of 4

mkrebs
Participant
Participant

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();
}

0 Likes
Message 4 of 4

Anonymous
Not applicable

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)]

 

 

0 Likes