Author Topic: How to aviod this exception?  (Read 2023 times)

0 Members and 1 Guest are viewing this topic.

guohq

  • Newt
  • Posts: 84
How to aviod this exception?
« on: August 06, 2010, 09:45:25 AM »
 Public Function CurveLength(ByVal ID As ObjectId, ByRef Len As Double) As Boolean
 
      CurveLength = True
        Dim DB As Database = HostApplicationServices.WorkingDatabase

        Using Trans As Transaction = DB.TransactionManager.StartTransaction

            Dim Ent As Entity = Trans.GetObject(ID, OpenMode.ForRead)

            Try
                Dim Cur As Curve = Ent  'Error?
                Len = Cur.GetDistanceAtParameter(Cur.EndParam)
            Catch ex As Exception
                CurveLength = False
            End Try
        End Using
    End Function

Dim Cur As Curve = Ent   'If Ent is not a linear entity,the function will not go on. why the function not jump to "catch" ?
« Last Edit: August 06, 2010, 10:12:19 AM by guohq »

dann.boy.001

  • Guest
Re: How to aviod this exception?
« Reply #1 on: August 06, 2010, 10:04:43 AM »

Hello!

I didn't test, but you can try:

 Public Function CurveLength(ByVal ID As ObjectId, ByRef Len As Double) As Boolean
 
      CurveLength = True
        Dim DB As Database = HostApplicationServices.WorkingDatabase

        Using Trans As Transaction = DB.TransactionManager.StartTransaction

            Dim Ent As Entity = Trans.GetObject(ID, OpenMode.ForRead)

            Try
                Dim Cur As Curve =trycast( Ent,curve)  'Error?
                Len = Cur.GetDistanceAtParameter(Cur.EndParam)
            Catch ex As Exception
                CurveLength = False
            End Try
        End Using
    End Function

kaefer

  • Guest
Re: How to aviod this exception?
« Reply #2 on: August 06, 2010, 10:27:23 AM »
Code: [Select]
            Try
                Dim Cur As Curve =[b]trycast( Ent,curve)[/b]  'Error?
                Len = Cur.GetDistanceAtParameter(Cur.EndParam)
            Catch ex As Exception
                CurveLength = False
            End Try

Why use a try/catch at all? The type test operator in vb is Is, as far as I know.
Code: [Select]
If (TypeOf ent Is Curve) Then ...
But what do I know...
Code: [Select]
// F# ahead
open Autodesk.AutoCAD.DatabaseServices
open Autodesk.AutoCAD.EditorInput
open Autodesk.AutoCAD.Runtime

type acApp = Autodesk.AutoCAD.ApplicationServices.Application

[<CommandMethod "test">]
let test() =
    let ed = acApp.DocumentManager.MdiActiveDocument.Editor
    let per = ed.GetEntity "\nSelect entity: "
    if per.Status = PromptStatus.OK then
        let db = ed.Document.Database
        use tr = db.TransactionManager.StartTransaction()
        match tr.GetObject(per.ObjectId, OpenMode.ForRead) with
        | :? Curve as cur ->
            let len = cur.GetDistanceAtParameter cur.EndParam
            "\nLength is " + string len |> ed.WriteMessage
        | _ -> "\nNo curve selected " |> ed.WriteMessage
        tr.Commit()

Regards

guohq

  • Newt
  • Posts: 84
Re: How to aviod this exception?
« Reply #3 on: August 06, 2010, 10:29:11 AM »
The CAD application will collapse when I run the function .

guohq

  • Newt
  • Posts: 84
Re: How to aviod this exception?
« Reply #4 on: August 06, 2010, 11:01:32 AM »
 Public Function CurveLength(ByVal ID As ObjectId, ByRef Len As Double) As Boolean
        CurveLength = True
        Dim DB As Database = HostApplicationServices.WorkingDatabase
        Using Trans As Transaction = DB.TransactionManager.StartTransaction

            Dim Ent As Entity = Trans.GetObject(ID, OpenMode.ForRead)
            Try
                If TypeOf Ent Is Curve Then
                    Dim Cur As Curve =  Ent
                    Len = Cur.GetDistanceAtParameter(Cur.EndParam)
                Else
                    CurveLength = False
                End If
            Catch ex As Exception
                CurveLength = False
            End Try
        End Using
    End Function