Author Topic: NumberStyles.AllowHexSpecifier  (Read 2081 times)

0 Members and 1 Guest are viewing this topic.

cadpro

  • Guest
NumberStyles.AllowHexSpecifier
« on: July 13, 2011, 06:42:43 AM »
Hi,

When converting handle string to ObjectId, I get an error when I use NumberStyles.AllowHexSpecifier eUnknownHandle in the Int64.Parse argument. It gives me an eUnknownHandle error. If I don't pass this argument, I get no errors. But it recognises the correct block. Would there be any problem later with other blocks?

Thanks

kaefer

  • Guest
Re: NumberStyles.AllowHexSpecifier
« Reply #1 on: July 13, 2011, 07:44:53 AM »
When converting handle string to ObjectId, I get an error when I use NumberStyles.AllowHexSpecifier eUnknownHandle in the Int64.Parse argument.

Uh, are you sure that it's Parse which balks here? Hint: try TryParse.

I had the following F# snippet put to good use; using the NumberStyles.HexNumber enum value, which combines NumberStyles.AllowHexSpecifier with leading and trailing whitespace flags.

Code: [Select]
    let GetBlockByHandle str =
        let db = HostApplicationServices.WorkingDatabase
        System.Int64.TryParse(
            str, System.Globalization.NumberStyles.HexNumber,
            System.Globalization.CultureInfo.InvariantCulture )
        |>  function
            | true, hex ->
                try
                    let objid = db.GetObjectId(false, new Handle(hex), 1)
                    Some(objid.GetObject OpenMode.ForRead :?> BlockReference)
                with
                    :? Autodesk.AutoCAD.Runtime.Exception as ex when ex.Message = "eWasErased" ->  None
            | _ -> None

The idea was to ignore malformed and nonexisteing handles, and to ignore erased block references as well.