polylines intersection

polylines intersection

carlosrdrzalns
Advocate Advocate
2,601 Views
5 Replies
Message 1 of 6

polylines intersection

carlosrdrzalns
Advocate
Advocate

 

I am trying to check if there is any MText below my current MText, as i have an exported drawing from Revit which explode every MText into single-line Mtext. So i have used this code:

 

 

private static bool IsTextBelow(Editor ed, MText entityabove, MText entitybelow )
        {
            Polyline pl = new Polyline();
            int[] VerticesX = new int[4] { 0, 1, 1, 0 };
            int[] VerticesY = new int[4] { 0, 0, -1, -1 };
            for(int i = 0; i == VerticesX.Length - 1 ;i++)
            {
                Point2d pnt = new Point2d(entityabove.Location.X + entityabove.ActualWidth * VerticesX[i], entityabove.Location.Y + (entityabove.ActualHeight + entityabove.TextHeight) * VerticesY[i]);
                pl.AddVertexAt(i, pnt, 0, 0, 0);
            }
            Polyline plaux = new Polyline();
            for (int i = 0; i == VerticesX.Length - 1; i++)
            {
                Point2d pnt = new Point2d(entitybelow.Location.X + entitybelow.ActualWidth * VerticesX[i], entitybelow.Location.Y + entitybelow.ActualHeight * VerticesY[i]);
                plaux.AddVertexAt(i, pnt, 0, 0, 0);
            }
            
            pl.Closed = true;
            plaux.Closed = true;
            
            Point3dCollection IntPnt = new Point3dCollection();
            pl.IntersectWith(plaux, Intersect.OnBothOperands, IntPnt, (int)IntPtr.Zero, (int)IntPtr.Zero);
            pl.Dispose();
            plaux.Dispose();
            if (IntPnt.Count > 0)
            {
                IntPnt.Dispose();
                return true;
            }
            else
            {
                IntPnt.Dispose();
                return false;
            }

        }

 

 

I am getting a weird error when using Entity.IntersectWith() method with two polylines. I have already use this code in VB.NET, but in this new proyect in C# i am getting this error:

 

 

Error: eInvalidInput en Acdbmgd , at Autodesk.AutoCAD.DatabaseServices.Entity.IntersectWith(Entity entityPointer, Intersect intersectType, Point3dCollection points, IntPtr thisGraphicSystemMarker, IntPtr otherGraphicSystemMarker)

 

My VB.NET code that already works is as following: 

 

 

 Private Function IsTextBelow(ByVal tx As MText, ByVal entitybelow As MText) As Boolean
        Try


            Dim pl As Polyline = New Polyline
            Dim VerticesX = New Integer() {0, 1, 1, 0}
            Dim VerticesY = New Integer() {0, 0, -1, -1}
            For i As Integer = 0 To VerticesX.Count - 1
                Dim pnt As Point2d = New Point2d(tx.Location.X + tx.ActualWidth * VerticesX(i), tx.Location.Y + (tx.ActualHeight + tx.TextHeight) * VerticesY(i))
                pl.AddVertexAt(i, pnt, 0, 0, 0)
            Next
            Dim plaux As Polyline = New Polyline
            For i As Integer = 0 To VerticesX.Count - 1
                Dim pnt As Point2d = New Point2d(entitybelow.Location.X + entitybelow.ActualWidth * VerticesX(i), entitybelow.Location.Y + (entitybelow.ActualHeight) * VerticesY(i))
                plaux.AddVertexAt(i, pnt, 0, 0, 0)
            Next
            plaux.Closed = True
            pl.Closed = True
            Dim IntPnt As Point3dCollection = New Point3dCollection
            pl.IntersectWith(plaux, Intersect.OnBothOperands, IntPnt, New IntPtr(0), New IntPtr(0))
            plaux.Dispose()
            pl.Dispose()

            If IntPnt.Count > 0 Then
                Return True
            Else
                Return False
            End If


        Catch ex As System.Exception
            ed.WriteMessage("error al encontrar el texto inferior")
            Return False
        End Try



    End Function

 

 

any clue about what may be happening?

 

Thanks in advance. 

 

 

0 Likes
Accepted solutions (1)
2,602 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant

Hi,

 

Except if you're targeting an old version of AutoCAD, you should not cast the IntPtr into an integer. If you absolutely need to cast the IntPtr, cast it into a long (Int64) instead of a int (Int32).

 

Try replacing this:

 

pl.IntersectWith(plaux, Intersect.OnBothOperands, IntPnt, (int)IntPtr.Zero, (int)IntPtr.Zero);

 

with this:

pl.IntersectWith(plaux, Intersect.OnBothOperands, IntPnt, IntPtr.Zero, IntPtr.Zero);

 

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 6

carlosrdrzalns
Advocate
Advocate

Thank you for your answer @_gile . 

 

I had already tried that before , but neither it works. I thought that i had posted the code with the newest version of intersetWith(), sorry for the missundertanding. Any other clue about where could be the problem?

 

Regards

0 Likes
Message 4 of 6

Alexander.Rivilis
Mentor
Mentor
Accepted solution

@carlosrdrzalns 

 

Replace next line:

for (int i = 0; i == VerticesX.Length - 1; i++)

with this line:

for (int i = 0; i < VerticesX.Length; i++)

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 5 of 6

carlosrdrzalns
Advocate
Advocate

Thank you @Alexander.Rivilis , that solved the problem. I thought that those conditions were the same. 

0 Likes
Message 6 of 6

Alexander.Rivilis
Mentor
Mentor

@carlosrdrzalns wrote:

Thank you @Alexander.Rivilis , that solved the problem. I thought that those conditions were the same. 


It will be wonderful to study the syntax of C#  😊

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member