<?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 Selece before running the rule and not after ? in Inventor Programming - iLogic, Macros, AddIns &amp; Apprentice</title>
    <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9969078#M119435</link>
    <description>&lt;P&gt;I have this ilogic code&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I force me to select an part in an assembly... but is it possible to select first and then run the code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my code now is this ... I want it to do the same ... but add the information for the part that are already select&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;Dim&lt;/SPAN&gt; &lt;SPAN&gt;oPart&lt;/SPAN&gt; &lt;SPAN&gt;As&lt;/SPAN&gt; &lt;SPAN&gt;ComponentOccurrence&lt;/SPAN&gt; = &lt;SPAN&gt;ThisApplication&lt;/SPAN&gt;.&lt;SPAN&gt;CommandManager&lt;/SPAN&gt;.&lt;SPAN&gt;Pick&lt;/SPAN&gt;(&lt;SPAN&gt;SelectionFilterEnum&lt;/SPAN&gt;.&lt;SPAN&gt;kAssemblyLeafOccurrenceFilter&lt;/SPAN&gt;, &lt;SPAN&gt;"Select FG component"&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 30 Dec 2020 10:59:04 GMT</pubDate>
    <dc:creator>Darkforce_the_ilogic_guy</dc:creator>
    <dc:date>2020-12-30T10:59:04Z</dc:date>
    <item>
      <title>Selece before running the rule and not after ?</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9969078#M119435</link>
      <description>&lt;P&gt;I have this ilogic code&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I force me to select an part in an assembly... but is it possible to select first and then run the code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my code now is this ... I want it to do the same ... but add the information for the part that are already select&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;Dim&lt;/SPAN&gt; &lt;SPAN&gt;oPart&lt;/SPAN&gt; &lt;SPAN&gt;As&lt;/SPAN&gt; &lt;SPAN&gt;ComponentOccurrence&lt;/SPAN&gt; = &lt;SPAN&gt;ThisApplication&lt;/SPAN&gt;.&lt;SPAN&gt;CommandManager&lt;/SPAN&gt;.&lt;SPAN&gt;Pick&lt;/SPAN&gt;(&lt;SPAN&gt;SelectionFilterEnum&lt;/SPAN&gt;.&lt;SPAN&gt;kAssemblyLeafOccurrenceFilter&lt;/SPAN&gt;, &lt;SPAN&gt;"Select FG component"&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Dec 2020 10:59:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9969078#M119435</guid>
      <dc:creator>Darkforce_the_ilogic_guy</dc:creator>
      <dc:date>2020-12-30T10:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: Selece before running the rule and not after ?</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9969180#M119439</link>
      <description>&lt;P&gt;Try this code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Dim asm As AssemblyDocument = ThisDoc.Document

Dim occ As ComponentOccurrence = Nothing

For Each selectedItem In asm.SelectSet
	Dim selectedOccurrence As ComponentOccurrence
	Try
		selectedOccurrence = selectedItem
		occ = selectedOccurrence
		Exit For
	Catch
		Continue For
	End Try
Next

If occ Is Nothing Then
	occ = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select FG component")
End If

If occ Is Nothing Then
	'Raise exception or return
	Return
End If

'...
MsgBox(occ.Name)&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 30 Dec 2020 12:06:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9969180#M119439</guid>
      <dc:creator>Michael.Navara</dc:creator>
      <dc:date>2020-12-30T12:06:06Z</dc:date>
    </item>
    <item>
      <title>Re: Selece before running the rule and not after ?</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9976671#M119561</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1104556"&gt;@Michael.Navara&lt;/a&gt;&amp;nbsp;Instead of trying to assign the selected item to a predefined object type and see if an error occurs (try-catch), why don't you simply check the .type of the selected item?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5708980"&gt;@Darkforce_the_ilogic_guy&lt;/a&gt;&amp;nbsp;Please find below the first section of code used in my macros which support selecting an object before running. As in your case, the selected object should be an occurrence. In my case, the user is only allowed to select one item.&lt;/P&gt;&lt;P&gt;Specific checks on the occurrence (is it visible, suppressed or else) are placed after this section.&amp;nbsp;It's VBA, but&amp;nbsp;it should be easily converted into iLogic.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;'Declarations
Dim oSourceOcc As ComponentOccurrence

'Get the selected occurrence
If ThisApplication.ActiveDocument.SelectSet.Count = 0 Then

  'Let user select a component
  Set oSourceOcc = ThisApplication.CommandManager.Pick(kAssemblyLeafOccurrenceFilter, "Select a component (or press ESC to quit)")

  'Quit if user pressed ESC
  If oSourceOcc Is Nothing Then
    End
  Else
    Call ThisApplication.ActiveDocument.SelectSet.Select(oSourceOcc)
  End If

ElseIf ThisApplication.ActiveDocument.SelectSet.Count &amp;gt; 1 Then

  'Inform user about too much selection
  MsgBox "Please select only one component at a time.", vbCritical + vbOKOnly, "&amp;lt;Macro Name&amp;gt;"
  End

Else

  'Check if selected item is an occurrence
  If Not ThisApplication.ActiveDocument.SelectSet(1).Type = kComponentOccurrenceObject And Not ThisApplication.ActiveDocument.SelectSet(1).Type = kComponentOccurrenceProxyObject Then

    'Inform user about incompatible selection
    MsgBox "Can't use this type of object." &amp;amp; vbCrLf &amp;amp; vbCrLf &amp;amp; "Please select a part or sub assembly and try again", vbCritical + vbOKOnly, "&amp;lt;Function Name&amp;gt;"
    End
  Else

    'Set source occurrence
    Set oSourceOcc = ThisApplication.ActiveDocument.SelectSet(1)
  End If
End If&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jan 2021 15:37:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9976671#M119561</guid>
      <dc:creator>_dscholtes_</dc:creator>
      <dc:date>2021-01-04T15:37:59Z</dc:date>
    </item>
    <item>
      <title>Re: Selece before running the rule and not after ?</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9978577#M119578</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5406798"&gt;@_dscholtes_&lt;/a&gt;&amp;nbsp; I use Try-Catch beacuse this has better performance in usual way and this is well known construct. When you use .Type check, VB.NET (iLogic) compiler creates late binding construct in background and this has worse performance. See&amp;nbsp;&lt;A href="https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/early-late-binding/" target="_blank"&gt;https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/early-late-binding/&lt;/A&gt;&amp;nbsp;for more information.&lt;/P&gt;&lt;P&gt;The best solution is use of TryCast. This has the same performance in usual way as Try-Catch and much better in unusual way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And also I use C# fo common work and there is not possible to use .Type check directly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bellow&amp;nbsp; is benchmark code sample. Try run this code in part sketch environment and in model environment.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expected results&lt;/P&gt;&lt;P&gt;In PlanarSketch [ms]&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;DEBUG|Try-Catch: 0,9974&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;DEBUG|Late binding: 25,932&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;DEBUG|TryCast: 0,9942&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In Model [ms]&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;DEBUG|Try-Catch: 370,0105&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;DEBUG|Late binding: 19,9461&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;DEBUG|TryCast: 3,9891&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Dim oSketch As PlanarSketch
Dim start As DateTime
Dim span As TimeSpan
Dim count = 10000
Dim activeEditObject = ThisApplication.ActiveEditObject

'Try-Catch
start = DateTime.Now
For i = 1 To count
	Try
		oSketch = activeEditObject
	Catch
		Continue For
	End Try
Next
span = DateTime.Now - start
Logger.Debug("Try-Catch: " &amp;amp; span.TotalMilliseconds)

'Late binding
start = DateTime.Now
For i = 1 To count
	If (activeEditObject.Type = ObjectTypeEnum.kPlanarSketchesObject) Then
		oSketch = ThisApplication.ActiveEditObject
	Else
		Continue For
	End If
Next
span = DateTime.Now - start
Logger.Debug("Late binding: " &amp;amp; span.TotalMilliseconds)

'TryCast
start = DateTime.Now
For i = 1 To count
	oSketch = TryCast(activeEditObject, PlanarSketch)
	If oSketch Is Nothing Then Continue For
Next
span = DateTime.Now - start
Logger.Debug("TryCast: " &amp;amp; span.TotalMilliseconds)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jan 2021 09:02:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9978577#M119578</guid>
      <dc:creator>Michael.Navara</dc:creator>
      <dc:date>2021-01-05T09:02:19Z</dc:date>
    </item>
    <item>
      <title>Re: Selece before running the rule and not after ?</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9978653#M119581</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1104556"&gt;@Michael.Navara&lt;/a&gt;&amp;nbsp;I can't run the code. It gives an error on the line starting with Logger. I don't think it's supported in Inventor 2018.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have read various comments on StackOverflow saying that using try-catch has a performance impact (same for using the VBA equivalent of 'on error'). That is clearly shown in the&amp;nbsp;'In model' numbers you have provided, which I think come closest to the user case of bt.&amp;nbsp;(I don't think he will be selecting 1000 objects, though).&lt;BR /&gt;&lt;BR /&gt;TryCast is the overall winner. I didn't know about this operator (and it's even available in VBA). I'm sure to check it out, thanks.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jan 2021 09:54:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9978653#M119581</guid>
      <dc:creator>_dscholtes_</dc:creator>
      <dc:date>2021-01-05T09:54:09Z</dc:date>
    </item>
    <item>
      <title>Re: Selece before running the rule and not after ?</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9986397#M119718</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5708980"&gt;@Darkforce_the_ilogic_guy&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After selection of part, selected part can be obtained from below code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument

Dim obj As Object
obj = oDoc.SelectSet.Item(1)&lt;/LI-CODE&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks and regards,&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2021 05:09:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/selece-before-running-the-rule-and-not-after/m-p/9986397#M119718</guid>
      <dc:creator>chandra.shekar.g</dc:creator>
      <dc:date>2021-01-08T05:09:52Z</dc:date>
    </item>
  </channel>
</rss>

