Skip to content

Commit

Permalink
published 1633_show_elements.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremytammik committed Mar 8, 2018
1 parent c29b67e commit 2b29b35
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
58 changes: 30 additions & 28 deletions a/1633_show_elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@
#RevitAPI @AutodeskRevit #bim #dynamobim @AutodeskForge #ForgeDevCon
–
...
A recent discussion on using the <code>ShowElements</code> method to toggle between documents and views brought up a few interesting points
&ndash; Open and active an unsaved document
&ndash; Zoom to selected elements
&ndash; Toggle between documents and views...
--->

### Switch View or Document by Showing Elements

A recent discussion on using the `ShowElements` method to toggle between documents and views brought up a few interesting points:

- [Open and active an unsaved document](#2)
- [Zoom to selected elements](#3)
- [Toggle between documents and views](#4)
- [Open and active an unsaved document](#2)
- [Zoom to selected elements](#3)
- [Toggle between documents and views](#4)

####<a name="2"></a>Open and Active an Unsaved Document

Expand All @@ -48,7 +50,7 @@ in [how to open and active a new document that is not saved](https://forums.auto
**Question:** When you have 2 Revit projects open and want to switch between them, it can be done with:

<pre class="code">
application.OpenAndActivateDocument(file).
application.OpenAndActivateDocument(file);
</pre>

I used `Document.PathName` to get the filename required.
Expand Down Expand Up @@ -83,9 +85,9 @@ on [mirroring in a new family and changing active view](http://thebuildingcoder.
I implemented a new external
command [CmdSwitchDoc](https://github.com/jeremytammik/the_building_coder_samples/blob/master/BuildingCoder/BuildingCoder/CmdSwitchDoc.cs)
in [The Building Coder samples](https://github.com/jeremytammik/the_building_coder_samples) to try this out, in
[release 2018.0.138.0](https://github.com/jeremytammik/the_building_coder_samples/releases/tag/2018.0.138.0).
[release 2018.0.138.1](https://github.com/jeremytammik/the_building_coder_samples/releases/tag/2018.0.138.1).

It demonstrates two uses of the the `ShowElements` method:
It demonstrates two uses of the `ShowElements` method:

- [Zoom to selected elements](#3)
- [Toggle between documents and views](#4)
Expand All @@ -107,7 +109,7 @@ It demonstrates two uses of the the `ShowElements` method:
&nbsp;&nbsp;<span style="color:#2b91af;">ElementSet</span>&nbsp;elements&nbsp;)
{
&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;n&nbsp;=&nbsp;ids.Count;

&nbsp;&nbsp;<span style="color:blue;">if</span>(&nbsp;0&nbsp;==&nbsp;n&nbsp;)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;message&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;select&nbsp;at&nbsp;least&nbsp;one&nbsp;element&nbsp;to&nbsp;zoom&nbsp;to.&quot;</span>;
Expand All @@ -120,17 +122,17 @@ It demonstrates two uses of the the `ShowElements` method:
&nbsp;&nbsp;<span style="color:blue;">catch</span>
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Document</span>&nbsp;doc&nbsp;=&nbsp;uidoc.Document;

&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>(&nbsp;<span style="color:#2b91af;">ElementId</span>&nbsp;id&nbsp;<span style="color:blue;">in</span>&nbsp;ids&nbsp;)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Element</span>&nbsp;e&nbsp;=&nbsp;doc.GetElement(&nbsp;id&nbsp;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elements.Insert(&nbsp;e&nbsp;);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;message&nbsp;=&nbsp;<span style="color:blue;">string</span>.Format(&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Cannot&nbsp;zoom&nbsp;to&nbsp;element{0}.&quot;</span>,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;==&nbsp;n&nbsp;?&nbsp;<span style="color:#a31515;">&quot;&quot;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&quot;s&quot;</span>&nbsp;);

&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Result</span>.Failed;
&nbsp;&nbsp;}
&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Result</span>.Succeeded;
Expand All @@ -140,7 +142,7 @@ It demonstrates two uses of the the `ShowElements` method:
This functionality is similar to that provided by
the [Zoom to Awesome! add-in](https://bimopedia.com/2013/04/02/zoom-to-awesome) by Phil Read.

Here is a 40-second demo by Luke Johnson
Here is a 40-second demo by Luke Johnson
of [using Zoom to Awesome](https://knowledge.autodesk.com/support/revit-products/getting-started/caas/screencast/Main/Details/8e9a043d-9383-496b-8e86-6ec3ab055c0e.html),
also showing how to add a keyboard shortcut:

Expand All @@ -162,23 +164,23 @@ also showing how to add a keyboard shortcut:
&nbsp;&nbsp;<span style="color:#2b91af;">UIDocument</span>&nbsp;uidoc&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UIDocument</span>(&nbsp;doc&nbsp;);
&nbsp;&nbsp;<span style="color:#2b91af;">Application</span>&nbsp;app&nbsp;=&nbsp;doc.Application;
&nbsp;&nbsp;<span style="color:#2b91af;">UIApplication</span>&nbsp;uiapp&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UIApplication</span>(&nbsp;app&nbsp;);

&nbsp;&nbsp;<span style="color:green;">//&nbsp;Select&nbsp;some&nbsp;elements&nbsp;in&nbsp;the&nbsp;first&nbsp;document</span>

&nbsp;&nbsp;<span style="color:#2b91af;">ICollection</span>&lt;<span style="color:#2b91af;">ElementId</span>&gt;&nbsp;idsView1
&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FilteredElementCollector</span>(&nbsp;doc,&nbsp;view1.Id&nbsp;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WhereElementIsNotElementType()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToElementIds();

&nbsp;&nbsp;<span style="color:green;">//&nbsp;Open&nbsp;the&nbsp;second&nbsp;file</span>

&nbsp;&nbsp;<span style="color:#2b91af;">UIDocument</span>&nbsp;uidoc2&nbsp;=&nbsp;uiapp
&nbsp;&nbsp;&nbsp;&nbsp;.OpenAndActivateDocument(&nbsp;filepath2&nbsp;);

&nbsp;&nbsp;<span style="color:#2b91af;">Document</span>&nbsp;doc2&nbsp;=&nbsp;uidoc2.Document;

&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;something&nbsp;in&nbsp;second&nbsp;file</span>

&nbsp;&nbsp;<span style="color:blue;">using</span>(&nbsp;<span style="color:#2b91af;">Transaction</span>&nbsp;tx&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Transaction</span>(&nbsp;doc2&nbsp;)&nbsp;)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;tx.Start(&nbsp;<span style="color:#a31515;">&quot;Change&nbsp;Scale&quot;</span>&nbsp;);
Expand All @@ -187,33 +189,33 @@ also showing how to add a keyboard shortcut:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Set(&nbsp;20&nbsp;);
&nbsp;&nbsp;&nbsp;&nbsp;tx.Commit();
&nbsp;&nbsp;}

&nbsp;&nbsp;<span style="color:green;">//&nbsp;Save&nbsp;modified&nbsp;second&nbsp;file</span>

&nbsp;&nbsp;<span style="color:#2b91af;">SaveAsOptions</span>&nbsp;opt&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SaveAsOptions</span>
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;OverwriteExistingFile&nbsp;=&nbsp;<span style="color:blue;">true</span>
&nbsp;&nbsp;};

&nbsp;&nbsp;doc2.SaveAs(&nbsp;filepath2,&nbsp;opt&nbsp;);

&nbsp;&nbsp;<span style="color:green;">//&nbsp;Switch&nbsp;back&nbsp;to&nbsp;original&nbsp;file;</span>
&nbsp;&nbsp;<span style="color:green;">//&nbsp;in&nbsp;a&nbsp;new&nbsp;file,&nbsp;doc.PathName&nbsp;is&nbsp;empty</span>

&nbsp;&nbsp;<span style="color:blue;">if</span>(&nbsp;!<span style="color:blue;">string</span>.IsNullOrEmpty(&nbsp;doc.PathName&nbsp;)&nbsp;)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;uiapp.OpenAndActivateDocument(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;doc.PathName&nbsp;);

&nbsp;&nbsp;&nbsp;&nbsp;doc2.Close(&nbsp;<span style="color:blue;">false</span>&nbsp;);&nbsp;<span style="color:green;">//&nbsp;no&nbsp;problem&nbsp;here,&nbsp;says&nbsp;Remy</span>
&nbsp;&nbsp;}
&nbsp;&nbsp;<span style="color:blue;">else</span>
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Avoid&nbsp;using&nbsp;OpenAndActivateDocument</span>

&nbsp;&nbsp;&nbsp;&nbsp;uidoc.ShowElements(&nbsp;idsView1&nbsp;);
&nbsp;&nbsp;&nbsp;&nbsp;uidoc.RefreshActiveView();

&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//doc2.Close(&nbsp;false&nbsp;);&nbsp;//&nbsp;Remy&nbsp;says:&nbsp;Revit&nbsp;throws&nbsp;the&nbsp;exception&nbsp;and&nbsp;doesn&#39;t&nbsp;close&nbsp;the&nbsp;file</span>
&nbsp;&nbsp;}
}
Expand Down
1 change: 1 addition & 0 deletions a/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3221,6 +3221,7 @@ <h3>Table of Contents</h3>
<tr><td align="right">1630</td><td>2018-03-01</td><td><a href="http://thebuildingcoder.typepad.com/blog/2018/03/updated-adn-web-site-revit-api-labs-and-tag-creation.html">Updated ADN Web Site, Revit API Labs, Tag Creation</a>&nbsp;&nbsp;&nbsp;<a href="1630_adn_web_labs_update.html">^</a>&nbsp;&nbsp;</td><td>2018, Getting Started, Labs, Migration, News, SDK Samples, Training, Update</td></tr>
<tr><td align="right">1631</td><td>2018-03-02</td><td><a href="http://thebuildingcoder.typepad.com/blog/2018/03/export-geometry-and-snoop-stable-representation-of-reference.html">Export Geometry and Snoop Stable Representation</a>&nbsp;&nbsp;&nbsp;<a href="1631_snoop_stable_rep.html">^</a>&nbsp;&nbsp;</td><td>.NET, Element Relationships, Export, Geometry, RevitLookup, Utilities</td></tr>
<tr><td align="right">1632</td><td>2018-03-06</td><td><a href="http://thebuildingcoder.typepad.com/blog/2018/03/create-2d-arc-and-filter-for-intersecting-elements.html">Create 2D Arc and Filter for Intersecting Elements</a>&nbsp;&nbsp;&nbsp;<a href="1632_filter_intersect.html">^</a>&nbsp;&nbsp;</td><td>Algorithm, Data Access, Element Creation, Family, Filters, Geometry, Performance</td></tr>
<tr><td align="right">1633</td><td>2018-03-08</td><td><a href="http://thebuildingcoder.typepad.com/blog/2018/03/switch-view-or-document-by-showing-elements.html">Switch View or Document by Showing Elements</a>&nbsp;&nbsp;&nbsp;<a href="1633_show_elements.html">^</a>&nbsp;&nbsp;</td><td>Algorithm, Utilities, View</td></tr>
</td></tr>
</table>

Expand Down

0 comments on commit 2b29b35

Please sign in to comment.