<?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: Can I use a line object in a parallel.foreach loop? in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9436127#M19862</link>
    <description>&lt;P&gt;Thanks, I didnt even think about this but when you mentioned it I thought that might be referring to efficiency. I guess I have to treat higher languages like a microcontroller&lt;/P&gt;</description>
    <pubDate>Sat, 11 Apr 2020 10:30:27 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2020-04-11T10:30:27Z</dc:date>
    <item>
      <title>Can I use a line object in a parallel.foreach loop?</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9427396#M19856</link>
      <description>&lt;P&gt;I have a list of line objects stored in RAM and I want to see if any of them (one always will be) are parallel to a seperate line object which is also already on RAM so I don't have to read them from the database. I also have another line object that will cross the parallel line from the list but start at the beginning of the separate line and end at the opposite side of the line from the list as shown in the image below. List line is red and the two comparisons lines are green.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="New Bitmap Image.jpg" style="width: 400px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/750948i46B2C3F17E26A072/image-size/medium?v=v2&amp;amp;px=400" role="button" title="New Bitmap Image.jpg" alt="New Bitmap Image.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it will add the line that is parallel but only when its partner intersects to a concurrentbag. The problem I get is that it either does not find a value or it finds a value and repeats that line 162 times (the amount of lines I have in the list). It is random if does or does not find a result. I tied using a regular loop and it has the same problem. Not sure where I am going wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Concurrent;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;

namespace placePillars.Place_Cables
{
    class CableIntersects
    {
        public static (Line, ObjectId) IntersectingCables(Document doc, Editor ed, Database db, List&amp;lt;Line&amp;gt; lineList, Line innerLine, Line outerLine)
        {
            //Assume the line is correct, ie y2-y
            bool innerbackwars = false;
            bool outerbackwars = false;
            bool linebackwards = false;
            Double mBoundary;
            Double bBoundary;
            Double mInner;
            Double mOuter;
            
            double tolerance = 0.01;

            ConcurrentBag&amp;lt;Line&amp;gt; newCable = new ConcurrentBag&amp;lt;Line&amp;gt;();
            ObjectId cableIntersect = new ObjectId();
            Line newcableResult = new Line();

            //Check where the start point is in regards to the end point
            if (innerLine.EndPoint.X - innerLine.StartPoint.X &amp;lt; 0)
            {
                //start point is after end point
                innerbackwars = true;
            }
            if (outerLine.EndPoint.X - outerLine.StartPoint.X &amp;lt; 0)
            {
                outerbackwars = true;
            }

            //Workout gradient of unchanging lines
            if (innerbackwars == false)
            {

                mInner = (innerLine.EndPoint.Y - innerLine.StartPoint.Y) / (innerLine.EndPoint.X - innerLine.StartPoint.X);
            }
            else
            {
                mInner = (innerLine.StartPoint.Y - innerLine.EndPoint.Y) / (innerLine.StartPoint.X - innerLine.EndPoint.X);;
            }

            if (outerbackwars == false)
            {

                mOuter = (outerLine.EndPoint.Y - outerLine.StartPoint.Y) / (outerLine.EndPoint.X - outerLine.StartPoint.X);
            }
            else
            {
                mOuter = (outerLine.StartPoint.Y - outerLine.EndPoint.Y) / (outerLine.StartPoint.X - outerLine.EndPoint.X);
            }


            //Workout y axis intersect (where x=0)
            Double bInner = innerLine.StartPoint.Y - mInner * innerLine.StartPoint.X;
            Double bOuter = outerLine.StartPoint.Y - mOuter * outerLine.StartPoint.X;

            try
            {
                //using (DocumentLock acLckDoc = doc.LockDocument())
                //{
                    //using (Transaction acTrans = db.TransactionManager.StartTransaction())
                    //{
                        //Check for parallel lines first
                        //Parallel.ForEach(lineList, (lineRecord) =&amp;gt;
                        foreach(Line lineRecord in lineList)
                        {
                            bool inner = false;
                            bool outer = false;

                            if (lineRecord.EndPoint.X - lineRecord.StartPoint.X &amp;lt; 0)

                            {
                                //start point is after end point
                                linebackwards = true;
                            }

                            //Workout gradients of the lines
                            if (linebackwards == false)
                            {

                                mBoundary = (lineRecord.EndPoint.Y - lineRecord.StartPoint.Y) / (lineRecord.EndPoint.X - lineRecord.StartPoint.X);
                            }
                            else
                            {
                                mBoundary = (lineRecord.StartPoint.Y - lineRecord.EndPoint.Y) / (lineRecord.StartPoint.X - lineRecord.EndPoint.X);
                            }
                            bBoundary = lineRecord.StartPoint.Y - mBoundary * lineRecord.StartPoint.X;

                            //test to see if  innter out outer intersect
                            //Check to see if they are parallel to reduce calculations
                            //y=xm+b ---&amp;gt; xm+b=y=xm+b ---&amp;gt; x=x (move x to LHS) ---&amp;gt; x+x(-1)
                            var x = mBoundary + mInner * -1;
                            var b = bBoundary * -1 + bInner;
                            if (x &amp;lt;= tolerance &amp;amp;&amp;amp; x &amp;gt;= -tolerance) x = 0;
                            double x1 = (float)b / (float)x;

                            x = mBoundary + mOuter * -1;
                            b = bBoundary * -1 + bOuter;
                            if (x &amp;lt;= tolerance &amp;amp;&amp;amp; x &amp;gt;= -tolerance) x = 0;
                            double x2 = (float)b / (float)x;

                            var _y = lineRecord.Handle.Value;
                            if (_y==67397683)
                            {
                                var _u = 1;
                            }

                            //If X1 is the intercept line then we want to look at ir whilst X2 = inf
                            if (!double.IsInfinity(x1) &amp;amp;&amp;amp; double.IsInfinity(x2))
                            {
                                //Dont know if line start or end comes first on x axis so work it out based on line length from x=0
                                if (innerbackwars == true)
                                {
                                    if (x1 &amp;lt; innerLine.StartPoint.X &amp;amp;&amp;amp; x1 &amp;gt; innerLine.EndPoint.X)
                                    {
                                        //if inner line intercepts then we want the outer as our cable
                                        outer = true;
                                    }
                                }
                                if (innerbackwars == false)
                                {
                                    if (x1 &amp;gt; innerLine.StartPoint.X &amp;amp;&amp;amp; x1 &amp;lt; innerLine.EndPoint.X)
                                    {
                                        outer = true;
                                    }
                                }
                                //if (inner == true &amp;amp;&amp;amp; outer == false) break;
                            }
                            //Now see if outer cable intersects
                            if (double.IsInfinity(x1) &amp;amp;&amp;amp; !double.IsInfinity(x2))
                            {
                                //Dont know if line start or end comes first on x axis so work it out based on line length from x=0
                                if (outerbackwars == false)
                                {
                                    if (x2 &amp;lt; outerLine.StartPoint.X &amp;amp;&amp;amp; x2 &amp;gt; outerLine.EndPoint.X)
                                    {
                                        inner = true;

                                    }
                                }
                                if (outerbackwars == true)
                                {
                                    if (x2 &amp;gt; outerLine.StartPoint.X &amp;amp;&amp;amp; x2 &amp;lt; outerLine.EndPoint.X)
                                    {
                                        inner = true;
                                    }
                                }
                            }
                            if ((inner == true &amp;amp;&amp;amp; outer == false) || (inner == false &amp;amp;&amp;amp; outer == true))
                            {
                                if (inner == true)
                                {
                                    newCable.Add(innerLine);
                                    cableIntersect = outerLine.ObjectId;
                                    inner = false;
                                    outer = false;
                                }
                                else
                                {
                                    newCable.Add(outerLine);
                                    cableIntersect = innerLine.ObjectId;
                                    inner = false;
                                    outer = false;
                                }
                            }
                        }//);//End of loop
                    //}
                //}
                var newcacleCount = newCable.Count;
                if (newcacleCount == 0)
                {
                    var _a = 1;
                }
                //if (newcacleCount &amp;gt; 1) { }
                else 
                {
                    foreach(var item in newCable)
                    {
                        newcableResult = item;
                    }
                };
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
                MessageBox.Show(ex.Source);

            }
            return (newcableResult, cableIntersect) ;
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 14:00:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9427396#M19856</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-04-07T14:00:01Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use a line object in a parallel.foreach loop?</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9427448#M19857</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;AFAIK, you cannot use parallel computing with DBObjects, but it may be possible with some non-database resident geometric objects (I know it is possible with Point3d, I did it &lt;A href="http://www.theswamp.org/index.php?topic=44312.msg496412#msg496412" target="_blank" rel="noopener"&gt;here&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;You could try with LineSegment3d objects instead of Lines&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 14:18:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9427448#M19857</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-04-07T14:18:38Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use a line object in a parallel.foreach loop?</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9429781#M19858</link>
      <description>&lt;P&gt;Thanks Gile, I will have to change the Line object out. I assume i can just copy the x,y coordinates to a float if the 3Dpoint doesn't workout&lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2020 10:36:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9429781#M19858</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-04-08T10:36:58Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use a line object in a parallel.foreach loop?</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9431447#M19859</link>
      <description>&lt;P&gt;Regarding this snippet from your code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;if ((inner == true &amp;amp;&amp;amp; outer == false) || (inner == false &amp;amp;&amp;amp; outer == true))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is equivalent to:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;if (inner ^ outer) &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the ^ (exclusive or) operator returns true if one of its operands is true and the other is false.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Apr 2020 03:54:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9431447#M19859</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2020-04-09T03:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use a line object in a parallel.foreach loop?</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9432065#M19860</link>
      <description>&lt;P&gt;Thank you, I will change that. Would reduce processing needed at all?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Apr 2020 11:12:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9432065#M19860</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-04-09T11:12:13Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use a line object in a parallel.foreach loop?</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9434638#M19861</link>
      <description>&lt;P&gt;Yes reducing it to a single simple operation would be more efficient.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, comparing a Boolean value to True is redundant.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;if(x == true) &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is equivalent to&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;if(x) &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;if(x == false) &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is equivalent to&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;if(!x) &lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 10 Apr 2020 14:22:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9434638#M19861</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2020-04-10T14:22:48Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use a line object in a parallel.foreach loop?</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9436127#M19862</link>
      <description>&lt;P&gt;Thanks, I didnt even think about this but when you mentioned it I thought that might be referring to efficiency. I guess I have to treat higher languages like a microcontroller&lt;/P&gt;</description>
      <pubDate>Sat, 11 Apr 2020 10:30:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-i-use-a-line-object-in-a-parallel-foreach-loop/m-p/9436127#M19862</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-04-11T10:30:27Z</dc:date>
    </item>
  </channel>
</rss>

