Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Setting the Type Comments

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
mkrebs
2546 Views, 3 Replies

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

Setting the Type Comments

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

3 REPLIES 3
Message 2 of 4
Troy_Gates
in reply to: mkrebs

Troy_Gates
Advocate
Advocate
Accepted solution

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

0 Likes

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

Message 3 of 4
mkrebs
in reply to: Troy_Gates

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

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

Message 4 of 4
Anonymous
in reply to: mkrebs

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

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.

Post to forums  

Autodesk Design & Make Report