TheSwamp

Code Red => .NET => Topic started by: guohq on August 06, 2010, 09:45:25 AM

Title: How to aviod this exception?
Post by: guohq 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" ?
Title: Re: How to aviod this exception?
Post by: dann.boy.001 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
Title: Re: How to aviod this exception?
Post by: kaefer 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
Title: Re: How to aviod this exception?
Post by: guohq on August 06, 2010, 10:29:11 AM
The CAD application will collapse when I run the function .
Title: Re: How to aviod this exception?
Post by: guohq 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