Revit APP Blog

Information on Revit APP.

Browsing Posts published by Bruce

Now Revit APP wikihelp is online. The url is: http://wikihelp.revitapp.com.

The bbs will be eliminated later and will be totally replaced by wikihelp.

Please go to http://cn.revitapp.com/downloadcenter.php for download.

Family loader pro is on the way…

We don’t want to discuss how you can modify or set Revit built-in schedule, instead we want to discuss how we can leverage different tools to get most customized schedule. Most of you may suffer when try to customize Revit built-in schedule especially when you want to follow some localization requirements. Revit API doesn’t provide comprehensive access for schedule but it allows 3rd party developer to have almost fully control of elements’ parameters. In addition, Microsoft Excel has powerful customization capability for whatever kinds of schedule you want with plenty help documentation like MSDN. As a result, you have the data and the tool, what obstacle you still? :) (For excel help: MSDN-http://msdn.microsoft.com/en-us/library/wss56bz7%28v=vs.80%29.aspx or other documentation)

Here we want to share how you can get up-to-date excel worksheet instead of detail of how you need to write code for excel or get parameters. Revit API provides a wonderful mechanism as IUpdater to let API developer be notified when something is changed inside.

public class ParameterUpdater : IUpdater

{

AddInId addinID;

UpdaterId updaterID;

ElementId paramId;

public ParameterUpdater(AddInId id, BuiltInParameter param)

{

addinID = id;

paramId = new ElementId(param);

updaterID = new UpdaterId(addinID, new Guid(“6BBDEB2B-F83B-40df-9101-01A02DFC1A37″));

}

public void Execute(UpdaterData data)

{

Document doc = data.GetDocument();

// update your Excel worksheet…

}

public string GetAdditionalInformation() { return “Element Parameter Changed”; }

public ChangePriority GetChangePriority() { return ChangePriority.FloorsRoofsStructuralWalls; }

public UpdaterId GetUpdaterId() { return updaterID; }

public string GetUpdaterName() { return “Element Parameter Changed”; }

public ChangeType GetChangeType() { return Element.GetChangeTypeParameter(paramId); }

}

This class gets Revit model parameter change events through implementing IUpdater interface. All functions are must implemented interfaces except the GetChangeType which is for convenient. The most important interface is the Execute which will be called when subscribed events happen (here is the parameter value changed event). We can also subscribe Application.DocumentChanged but this event will be triggered for any change in the document. So we would strongly suggest to use IUpdater mechanism for this case.

Then in your own OnStartup interface function add following code:

ParameterUpdater updater = new ParameterUpdater(uiControlledApplication.ActiveAddInId, BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS);

if (!UpdaterRegistry.IsUpdaterRegistered(updater.GetUpdaterId()))

UpdaterRegistry.RegisterUpdater(updater);

ElementCategoryFilter pipeFilter = new ElementCategoryFilter(BuiltInCategory.OST_PipeCurves);

ElementIsElementTypeFilter nonTypeFilter = new ElementIsElementTypeFilter(true);

LogicalAndFilter filter = new LogicalAndFilter(pipeFilter, nonTypeFilter);

UpdaterRegistry.AddTrigger(updater.GetUpdaterId(), filter, updater.GetChangeType());

UpdaterRegistry.AddTrigger(updater.GetUpdaterId(), filter, Element.GetChangeTypeElementAddition());

UpdaterRegistry.AddTrigger(updater.GetUpdaterId(), filter, Element.GetChangeTypeElementDeletion());

Here we subscribed the Comments parameter which you can change to any parameter you want. Another thing I want to mention is that ParameterUpdater won’t be triggered when new element is added or element is deleted, so we also registered Element.GetChangeTypeElementAddition() and Element.GetChangeTypeElementDeletion() to track new element and element deletion.

I just updated Auto Dimension and Batch Material tools to support Revit 2012 release. Please check it http://en.revitapp.com/.

In addition, I got some idea about how we can get a much better family loader, and I’m working on that. A totally new family loader will come soon.

Before Revit 2010 API, there is no transaction exported to API developers, that’s to say, API developer doesn’t need to care about how transaction works for Revit document and how we should use it in API development. But for new Revit API, we have to create our own Transaction and we can decide when to start, commit or rollback our transaction. I’d like to discuss a little more about the transaction in Revit API development because many of us met problems for this.

Transaction in Revit API is just like what we now a transaction is. Transaction is the minimal atom of user visible change in document – Undo/Redo. All changes in a transaction will be combined to one Undo/Redo item in Revit, user can easily undo/redo the block of changes, but there is no way to undo/redo part of changes in a transaction – they have to be done together or be cancelled together.

In new Revit API, any change to document needs be included in transaction, if you forgot to do so, a warning will pop up when you execute your own addins. So remember to make sure all changes are in transaction(s) (It’s not necessary to put all changes in one big transaction, but any change should be in a transaction).

To add a transaction is quite simple:

Transaction myTrans = new Transaction(doc);
myTrans.Start(“My Change”);

The string parameter in the “Start” function is the name of this transaction and will be shown in the Undo/Redo list, so choose a proper transaction name is useful for end user to understand what happened.

To commit or rollback a transaction is also simple:

myTrans.commit(); or myTrans.rollback();

We have two methods to control whether we will show or hide elements in a specific view:

View.setVisibility / View.getVisibility and View.Hide / View.Unhide. The first pair is to control whether all elements belong to a specific category will show, the second pair is to control whether a specific group of elements will show in a view.

Here is some sample code:

UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Application app = commandData.Application.Application;
Document doc = uiDoc.Document;

View curView = doc.ActiveView;
Transaction trans = new Transaction(doc);
trans.Start(“Hide or Unhide”);
Category cat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Doors);
if (cat != null)
{
bool bVisibility = curView.getVisibility(cat);
curView.setVisibility(cat, !bVisibility);
}

ElementSet elemSet = new ElementSet();
ICollection<Element> elems = new FilteredElementCollector(doc, curView.Id).OfCategory(BuiltInCategory.OST_Windows).WhereElementIsNotElementType().ToElements();
bool bHide = true;
foreach (Element e in elems)
{
elemSet.Insert(e);
bHide = e.IsHidden(curView);
}
if (bHide == false)
{
curView.Hide(elemSet);
}
trans.Commit();
return Result.Succeeded;

Attention:

Once you hide some elements, you cannot be able to FilteredElementCollector(doc, curView.Id) to get these elements, so you’d better to store these elements’ ids somewhere if you want to unhide them later.

Some times we need retrieve all level elevatioins, here is the code:

string strMsg = “”;
Transaction trans = new Transaction(doc, “Show Level”);
trans.Start();
FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> elems = collector.WhereElementIsNotElementType().OfClass(typeof(Level)).ToElements();
foreach (Level lv in elems)
{
strMsg += lv.Name + ” : elevation” + lv.Elevation.ToString(“F2″) + “\n”;
}
TaskDialog.Show(“Level elements”, strMsg);
trans.RollBack();

Just notice that, the Elevation got from the Level element is stored in feet unit, if you want to show other unit (like inches or meters), you need to transform it before use it.

All materials in a document is stored in doc.Settings.Materials (doc is the pointer of the document). You can get all available materials through this property.

Material’s specified properties:

Color The color of the material.
CutPattern The cut pattern of the material.
CutPatternColor The cut pattern color of the material.
Glow Whether the material can glow.
RenderAppearance The rendering appearance property of the material.
Shininess The shininess of the material.
Smoothness The smoothness of the material.
SurfacePattern The surface pattern of the material.
SurfacePatternColor The surface pattern color of the material.
Transparency The transparency of the material.

You can modify a meterial through these properties. Of course, you can also add/insert/remove materials for doc.Settings.Materials.

Powered by WordPress Web Design by SRS Solutions © 2012 Revit APP Blog Design by SRS Solutions