<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Need help with efficiency and elegance in simple code in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8410915#M46185</link>
    <description>&lt;P&gt;1)change list to array, you don't have to change foreach to for because compiler usually do it for you, but remember that with out optimization of code for is faster than a foreach.&lt;/P&gt;&lt;P&gt;2) delete&lt;/P&gt;&lt;PRE&gt;using (Transaction t = new Transaction(doc, "parameters"))&lt;/PRE&gt;&lt;P&gt;why this is even here ? you put using transaction in to using transaction, why?&lt;/P&gt;&lt;P&gt;3) put loop in side transaction not out side(every start and commit take a lot of time)&lt;/P&gt;&lt;P&gt;4) if you get parameter with string property, you have to check if parameter is not null&lt;/P&gt;&lt;PRE&gt;if(s_parameter != null)&lt;/PRE&gt;&lt;P&gt;5)check if parameter is read only and if it takes double as value&lt;/P&gt;&lt;P&gt;6)so application can only Succeeded&lt;/P&gt;&lt;P&gt;7) do you change type or instance? you can .WhereElementIsElementType to a FillteredElementCollector&lt;/P&gt;</description>
    <pubDate>Mon, 19 Nov 2018 16:03:47 GMT</pubDate>
    <dc:creator>MarryTookMyCoffe</dc:creator>
    <dc:date>2018-11-19T16:03:47Z</dc:date>
    <item>
      <title>Need help with efficiency and elegance in simple code</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8410687#M46184</link>
      <description>&lt;P&gt;My code works but I don't believe it's as efficient as it could be. Specifically the Try/catch area of the code. If I don't try/catch each line separately it misses some of the doors. Like to reduce the regeneration time too if possible. Any pointers are appreciated. Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;            // Get doors
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            List&amp;lt;Element&amp;gt; coll = collector.OfClass(typeof(FamilyInstance))
                .OfCategory(BuiltInCategory.OST_Doors)
                .ToList();

            // Filtered element collector is iterable
            foreach (Element e in coll)
            {
                // Get the parameter name
                Parameter s_parameter = e.LookupParameter("Swing Angle");
                Parameter s1_parameter = e.LookupParameter("Swing Angle_Door 1");
                Parameter s2_parameter = e.LookupParameter("Swing Angle_Door 2");
                using (Transaction t = new Transaction(doc, "parameters"))

                // Modify document within a transaction
                using (Transaction tx = new Transaction(doc))
                {
                    tx.Start("Change door swing angles to 45");
                   
                    try
                    {
                        s_parameter.Set(0.785398163);
                    }
                    catch { }
                    try
                    {
                        s1_parameter.Set(0.785398163);
                    }
                    catch { }
                    try
                    { 
                        s2_parameter.Set(0.785398163);
                    }
                    catch { }
                    tx.Commit();
                }
            }
            TaskDialog.Show("Completed", "Door swings changed to 45°.");
            return Result.Succeeded;

&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Nov 2018 14:49:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8410687#M46184</guid>
      <dc:creator>62BJW</dc:creator>
      <dc:date>2018-11-19T14:49:04Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with efficiency and elegance in simple code</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8410915#M46185</link>
      <description>&lt;P&gt;1)change list to array, you don't have to change foreach to for because compiler usually do it for you, but remember that with out optimization of code for is faster than a foreach.&lt;/P&gt;&lt;P&gt;2) delete&lt;/P&gt;&lt;PRE&gt;using (Transaction t = new Transaction(doc, "parameters"))&lt;/PRE&gt;&lt;P&gt;why this is even here ? you put using transaction in to using transaction, why?&lt;/P&gt;&lt;P&gt;3) put loop in side transaction not out side(every start and commit take a lot of time)&lt;/P&gt;&lt;P&gt;4) if you get parameter with string property, you have to check if parameter is not null&lt;/P&gt;&lt;PRE&gt;if(s_parameter != null)&lt;/PRE&gt;&lt;P&gt;5)check if parameter is read only and if it takes double as value&lt;/P&gt;&lt;P&gt;6)so application can only Succeeded&lt;/P&gt;&lt;P&gt;7) do you change type or instance? you can .WhereElementIsElementType to a FillteredElementCollector&lt;/P&gt;</description>
      <pubDate>Mon, 19 Nov 2018 16:03:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8410915#M46185</guid>
      <dc:creator>MarryTookMyCoffe</dc:creator>
      <dc:date>2018-11-19T16:03:47Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with efficiency and elegance in simple code</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8411081#M46186</link>
      <description>&lt;P&gt;Thanks for the reply. I understand some of what you said. I'm pretty new to C#. I use to write VB but that was a few years ago. I can't seem to move the start/commit outside of the loop. See image.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Nov 2018 17:08:17 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8411081#M46186</guid>
      <dc:creator>62BJW</dc:creator>
      <dc:date>2018-11-19T17:08:17Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with efficiency and elegance in simple code</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8411199#M46187</link>
      <description>&lt;P&gt;you forget about {} after using, try this:&lt;/P&gt;&lt;PRE&gt;    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        private const double angle = 0.785398163;
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;
            // Get insatnce of doors
            Element[] coll = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors).WhereElementIsNotElementType().ToArray();
            //start transaction out side of loop, that way you only open transaction once
            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Change door swing angles to 45");
                foreach (Element e in coll)
                {
                    //set Parameter by the name
                    try
                    {
                        SetParameter(e, "Swing Angle", angle);
                        SetParameter(e, "Swing Angle_Door 1", angle);
                        SetParameter(e, "Swing Angle_Door 2", angle);
                    }
                    catch { }
                }
                tx.Commit();
            }
            TaskDialog.Show("Completed", "Door swings changed to 45°.");
            return Result.Succeeded;
        }
        /// &amp;lt;summary&amp;gt;
        /// set double parameter by value
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="element"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="Name"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="value"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        private static bool SetParameter(Element element, string Name, double value)
        {
            Parameter parameter = element.LookupParameter(Name);
            //preventing exceptions is better than catching it 
            if (parameter != null &amp;amp;&amp;amp; !parameter.IsReadOnly &amp;amp;&amp;amp; parameter.StorageType == StorageType.Double)
                return parameter.Set(value);
            return false;
        }
    }&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Nov 2018 17:52:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8411199#M46187</guid>
      <dc:creator>MarryTookMyCoffe</dc:creator>
      <dc:date>2018-11-19T17:52:20Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with efficiency and elegance in simple code</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8411264#M46188</link>
      <description>&lt;P&gt;Thanks works perfectly. Now I'll step through the code so I can learn what you did. Thanks Again!&lt;/P&gt;</description>
      <pubDate>Mon, 19 Nov 2018 18:19:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8411264#M46188</guid>
      <dc:creator>62BJW</dc:creator>
      <dc:date>2018-11-19T18:19:08Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with efficiency and elegance in simple code</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8412638#M46189</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Dear Bernie,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you for your query.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I am very glad you care about these aspects.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Very many thanks to MarryTookMyCoffe for jumping in and helping so efficiently!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I would like to add a few points more:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;LookupParameter will only retrieve the first parameter of the given name. In order to use it, you must be absolutely certain that only one parameter with the given name exists. I would suggest adding a test somewhere in your add-in startup code to ensure that this really is the case.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It is always safer to use other means to retrieve parameters, e.g., the built-in parameter enumeration value, if it exists, or simply the Parameter definition, which you can look up and cache beforehand, e.g., in the afore-mentioned code ensuring that the parameter name on that element type really is unique.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;There is no need to convert the element collection to an array, i.e., saying&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;  Element[] coll = new FilteredElementCollector(doc)...ToArray();&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You can perfectly well leave it as a filtered element collector and iterate directly over that instead:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;  FilteredElementCollector coll = new FilteredElementCollector(doc)...;
  foreach (Element e in coll) ...&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;That will save conversion time and space and avoid the unnecessary data duplication.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I hope this helps.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Best regards,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Jeremy&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Nov 2018 07:47:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/need-help-with-efficiency-and-elegance-in-simple-code/m-p/8412638#M46189</guid>
      <dc:creator>jeremytammik</dc:creator>
      <dc:date>2018-11-20T07:47:12Z</dc:date>
    </item>
  </channel>
</rss>

