A simple overrule snaps which either doesn't work or make crash

A simple overrule snaps which either doesn't work or make crash

JanetDavidson
Advocate Advocate
939 Views
5 Replies
Message 1 of 6

A simple overrule snaps which either doesn't work or make crash

JanetDavidson
Advocate
Advocate

Hello again. I hope this time somebody help me.

I tried to simplified everything .

 Attached please find a  a drawing  for use with code. command is  "onoff" to make over rule activate.

Problem is snaps doesn't like to work at all .  I try to put a dimension line at end of overrule lines and either it crash or doen't like to grip the end point of line. And below is code.  it is 2012 .

 

Thanks,

Janet.

 

Imports System.Math
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.GraphicsInterface
Public Class MyOverRuleClass
    Public Shared MyXdataTablename = "My_Data"
    Public Shared BlockshapeOverrule As Block_XdataDrawOverrule
    Public Shared BlockOsnapOverrule As Block_XdataOsnapOverrule
    <CommandMethod("onoff")> _
    Public Shared Sub StartMyOverrule()
        If BlockshapeOverrule Is Nothing Then
            BlockshapeOverrule = New Block_XdataDrawOverrule
            BlockOsnapOverrule = New Block_XdataOsnapOverrule
            Overrule.AddOverrule(RXClass.GetClass(GetType(BlockReference)), BlockshapeOverrule, True)
            Overrule.AddOverrule(RXClass.GetClass(GetType(BlockReference)), BlockOsnapOverrule, True)
            BlockshapeOverrule.SetXDataFilter(MyXdataTablename)
            BlockOsnapOverrule.SetXDataFilter(MyXdataTablename)
            Application.DocumentManager.MdiActiveDocument.Editor.Regen()
        Else
            Overrule.RemoveOverrule(RXClass.GetClass(GetType(BlockReference)), BlockshapeOverrule)
            BlockshapeOverrule.Dispose()
            BlockshapeOverrule = Nothing
            Application.DocumentManager.MdiActiveDocument.Editor.Regen()
        End If
    End Sub
End Class
Public Class Block_Dimension
    Inherits DrawableOverrule
    Public Overrides Function WorldDraw(ByVal drawable As Autodesk.AutoCAD.GraphicsInterface.Drawable, ByVal wd As Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean
        Return False
    End Function
End Class
Public Class Block_XdataDrawOverrule
    Inherits DrawableOverrule
    Public Overrides Function WorldDraw(ByVal drawable As Autodesk.AutoCAD.GraphicsInterface.Drawable, ByVal wd As Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean
        Return False
    End Function
    Public Overloads Overrides Sub ViewportDraw(ByVal drawable As Autodesk.AutoCAD.GraphicsInterface.Drawable, _
                                                ByVal vd As Autodesk.AutoCAD.GraphicsInterface.ViewportDraw)

        Dim myDynamicBlock As BlockReference = drawable

        Dim BlockOffset1 As New BlockOffset
        Dim ListEnts As List(Of Entity) = BlockOffset1.Offset(myDynamicBlock)
        For Each Ent As Entity In ListEnts
            Ent.ViewportDraw(vd)
            Ent.Dispose()
        Next
        MyBase.ViewportDraw(drawable, vd)
    End Sub
End Class
Public Class Block_XdataOsnapOverrule
    Inherits OsnapOverrule
    Public Overrides Sub GetObjectSnapPoints(ByVal e As Entity, _
                                              ByVal snapMode As ObjectSnapModes, _
                                              ByVal gsSelectionMark As System.IntPtr, _
                                              ByVal pickPoint As Point3d, _
                                              ByVal lastPoint As Point3d, _
                                              ByVal viewTransform As Matrix3d, _
                                              ByVal snapPoints As Point3dCollection, _
                                              ByVal geometryIds As IntegerCollection)

        Dim myDynamicBlock As BlockReference = TryCast(e, BlockReference)
        Dim BlockOffset1 As New BlockOffset
        Dim ListEnts As List(Of Entity) = BlockOffset1.Offset(myDynamicBlock)
        For Each SubEnt As Entity In ListEnts
            SubEnt.GetObjectSnapPoints(snapMode, gsSelectionMark, pickPoint, lastPoint, viewTransform, snapPoints, geometryIds)
            SubEnt.Dispose()
        Next
        MyBase.GetObjectSnapPoints(e, snapMode, gsSelectionMark, pickPoint, lastPoint, viewTransform, snapPoints, geometryIds)
    End Sub
End Class

Public Class BlockOffset
    Function Offset(ByVal MyBlock As BlockReference) As List(Of Entity)
        Offset = New List(Of Entity)
        Dim List_Entities As List(Of Entity) = GetBlockEntities(MyBlock)
        Dim line1 As New Line
        line1 = TryCast(List_Entities(0), Line)
        Dim MyPolyLine1 As Autodesk.AutoCAD.DatabaseServices.Polyline = New Autodesk.AutoCAD.DatabaseServices.Polyline
        Dim MyPt1 As Point3d = PolarPoint(line1.StartPoint, line1.Angle + (0.5 * PI), 300)
        Dim MyPt2 As Point3d = PolarPoint(line1.StartPoint, line1.Angle - (0.5 * PI), 300)
        MyPolyLine1.AddVertexAt(0, New Point2d(MyPt1.X, MyPt1.Y), 0, 0, 0)
        MyPolyLine1.AddVertexAt(1, New Point2d(MyPt2.X, MyPt2.Y), 0, 0, 0)
        Offset.Add(TryCast(MyPolyLine1, Entity)) ' perpendicular line to block direction at beginning

        MyPolyLine1 = New Autodesk.AutoCAD.DatabaseServices.Polyline
        MyPt1 = PolarPoint(line1.StartPoint, line1.Angle, 200)
        MyPolyLine1.AddVertexAt(0, New Point2d(line1.StartPoint.X, line1.StartPoint.Y), 0, 0, 0)
        MyPolyLine1.AddVertexAt(1, New Point2d(MyPt1.X, MyPt1.Y), 0, 0, 0)
        MyPolyLine1.SetEndWidthAt(0, 100)
        Offset.Add(TryCast(MyPolyLine1, Entity)) '  arrow at begining
    End Function
    Shared Function GetBlockEntities(ByVal BR As BlockReference) As List(Of Entity)
        GetBlockEntities = New List(Of Entity)
        Dim acDBObjColl As DBObjectCollection = New DBObjectCollection()
        BR.Explode(acDBObjColl)
        For Each Ent As DBObject In acDBObjColl
            If TypeOf Ent Is Line Then
                Dim MyEntity As Entity = TryCast(Ent, Entity).Clone
                GetBlockEntities.Add(MyEntity)
            End If
        Next
    End Function
    Private Function PolarPoint(ByVal basepoint As Point3d, ByVal angle As Double, ByVal distance As Double) As Point3d
        Return New Point3d(basepoint.X + (distance * Cos(angle)), basepoint.Y + (distance * Sin(angle)), basepoint.Z)
    End Function
End Class



 Edited by
Discussion_Admin

0 Likes
940 Views
5 Replies
Replies (5)
Message 2 of 6

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

first I see this line is missing when you turn off your overrule-handling.

Overrule.RemoveOverrule(RXClass.GetClass(GetType(BlockReference)), BlockOsnapOverrule) 'appended

 

And the rest: I don't see anything except dynamic block (within the zoomed area, outside of screen are a lot more entities) and what should I do to see your problem? I also don't know what this overrule should do (and I don't like to study your code, it's more efficient if you describe what to do to come to your problem 😉

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 6

JanetDavidson
Advocate
Advocate

Alfred,

Thanks for Reply, I added your suggestion line .

My intetion is zoomed area ignore the rest , . I added a picture below to show you what it does.

 

0 Likes
Message 4 of 6

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

thx for your screenshot!

 

When I load your app and start command ONOFF I get a lot of these messages:

Forgot to call Dispose? (Autodesk.AutoCAD.DatabaseServices.Polyline): DisposableWrapper
Forgot to call Dispose? (Autodesk.AutoCAD.DatabaseServices.Line): DisposableWrapper

That could be avoided and for more stability it is recommended to use .Dispose on such objects.

 

Next problem is that I get a System.ArgumentOutOfRangeException (plus a Framework error dialog, giving the information that this line:

line1 = TryCast(List_Entities(0), Line)

creates the exception, could be

a) the parameter for the GetBlockEntities-call (variable myBlock) is nothing

b) the BlockTableRecord does not contain LINES

c) or (at least) your function GetBlockEntities does not return values in any case or in the right way.

 

That is what I got know with Civil3D2012x64 under Win7x64 (all German).

 

Good luck, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 6

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

ok, I have tested now with a small part of your drawing after changing the code to:

 

- alfred -

   Function Offset(ByVal MyBlock As BlockReference) As List(Of Entity)
      Offset = New List(Of Entity)
      Dim List_Entities As List(Of Entity) = GetBlockEntities(MyBlock)
      If List_Entities.Count > 0 Then
         Dim line1 As New Line
         line1 = TryCast(List_Entities(0), Line)
         Dim MyPolyLine1 As Autodesk.AutoCAD.DatabaseServices.Polyline = New Autodesk.AutoCAD.DatabaseServices.Polyline
         Dim MyPt1 As Point3d = PolarPoint(line1.StartPoint, line1.Angle + (0.5 * PI), 300)
         Dim MyPt2 As Point3d = PolarPoint(line1.StartPoint, line1.Angle - (0.5 * PI), 300)
         MyPolyLine1.AddVertexAt(0, New Point2d(MyPt1.X, MyPt1.Y), 0, 0, 0)
         MyPolyLine1.AddVertexAt(1, New Point2d(MyPt2.X, MyPt2.Y), 0, 0, 0)
         Offset.Add(TryCast(MyPolyLine1, Entity)) ' perpendicular line to block direction at beginning

         MyPolyLine1 = New Autodesk.AutoCAD.DatabaseServices.Polyline
         MyPt1 = PolarPoint(line1.StartPoint, line1.Angle, 200)
         MyPolyLine1.AddVertexAt(0, New Point2d(line1.StartPoint.X, line1.StartPoint.Y), 0, 0, 0)
         MyPolyLine1.AddVertexAt(1, New Point2d(MyPt1.X, MyPt1.Y), 0, 0, 0)
         MyPolyLine1.SetEndWidthAt(0, 100)
         Offset.Add(TryCast(MyPolyLine1, Entity)) '  arrow at begining
      End If
   End Function

 I added the "if ....end if" in this snippet and for short test's it seems to work, no exception, no crash, but very short test!!!

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 6 of 6

JanetDavidson
Advocate
Advocate

Thank you Alfred, Really appreciate your kindness.

I applied you suggestion .  

 If List_Entities.Count > 0 Then

But Still it crashes  when I try to put a dimension like the one  shown on the  picture attached .

The one with attribute  and   EndPoint sanp is on.  Funny thing is If I try to draw a line and snaps to that point, it works fine just dimesnion doesn't  work.

 

I don't know is this because of dispose? Still I didn't apply your suggestion regard with dispose  because the other block which doens't have attribute is fine . 

Besides I don't know where the dispose is necessary . You are right . most of the time I get unexpected crashed every 2 hours and that could be becuase of that . But I don't really know where and at which line I have to do it ?

Thanks again Al.

Janet.

 

 

 

0 Likes