September 15, 2008

On Error Resume Next

Those four words (in the title above) can prove to be quite valuable if you use a Formula property to extract data from the object to which the property is attached. For example, suppose that you had a situation in a Door Schedule where you needed to know the "door type" of each door being scheduled. This is not available from an automatic property source, but can be retrieved in a Formula property, as previously shown here and here. The door type as a text string formula from the latter post, which works across external references for those using ADT 2006 or later, is reproduced here, for reference:
Set acadApp = GetObject(,"AutoCAD.Application")
Set doorObj = acadApp.ActiveDocument.ObjectIDToObject( [ObjectID] )
Set doorStyle = doorObj.Style
doorTypeInt = doorStyle.Type

Select Case doorTypeInt
Case "0"
RESULT = "Custom"
Case "1"
RESULT = "Single"
Case "2"
RESULT = "Double"
Case "3"
RESULT = "Single-Dhung"
Case "4"
RESULT = "Double-Dhung"
Case "5"
RESULT = "Double Opposing"
Case "6"
RESULT = "Uneven"
Case "7"
RESULT = "Uneven-Dhung"
Case "8"
RESULT = "Uneven Opposing"
Case "9"
RESULT = "Bifold"
Case "10"
RESULT = "Bifold Double"
Case "11"
RESULT = "Pocket"
Case "12"
RESULT = "Double Pocket"
Case "13"
RESULT = "Sliding Double"
Case "14"
RESULT = "Sliding Triple"
Case "15"
RESULT = "Overhead"
Case "16"
RESULT = "Revolving"
Case "17"
RESULT = "Pass Thru"
Case "18"
RESULT = "Accordion"
Case "19"
RESULT = "Panel"
Case "20"
RESULT = "Communicating"
Case Else
RESULT = "Unknown Door Type"
End Select


Suppose further that your "Door Schedule" is for a commercial project where the doors have hollow metal frames and that you have several borrowed lights that have no door, but which you want to include in the "Door Schedule" (making it more of an "Opening Schedule"). If you choose to use Door/Window Assemblies to model those borrowed lights, it is easy enough to make the Property Set Definition(s) and Schedule Table Style apply to both Doors and Door/Window Assemblies. Unfortunately, when you take a look at the value of that Formula property when it is attached to a Door/Window Assembly, you will find that the formula fails. The reason for this is that Door/Window Assemblies Styles do not have a "type" property, so the
doorTypeInt = doorStyle.Type
line in the formula fails.

This is where those four words come in handy. By placing them at the beginning of the formula, you are telling ACD-A/ADT that if an error condition occurs, to continue evaluating the formula starting with the line after the line where the error occurred. The doorTypeInt variable will not be set to a value, but that is acceptable, since we can use the Case Else statement to set a RESULT value when the style of the object to which the property is attached does not have a "type" property.
On Error Resume Next
Set acadApp = GetObject(,"AutoCAD.Application")
Set doorObj = acadApp.ActiveDocument.ObjectIDToObject( [ObjectID] )
Set doorStyle = doorObj.Style
doorTypeInt = doorStyle.Type

Select Case doorTypeInt
Case "0"
RESULT = "Custom"
Case "1"
RESULT = "Single"
Case "2"
RESULT = "Double"
Case "3"
RESULT = "Single-Dhung"
Case "4"
RESULT = "Double-Dhung"
Case "5"
RESULT = "Double Opposing"
Case "6"
RESULT = "Uneven"
Case "7"
RESULT = "Uneven-Dhung"
Case "8"
RESULT = "Uneven Opposing"
Case "9"
RESULT = "Bifold"
Case "10"
RESULT = "Bifold Double"
Case "11"
RESULT = "Pocket"
Case "12"
RESULT = "Double Pocket"
Case "13"
RESULT = "Sliding Double"
Case "14"
RESULT = "Sliding Triple"
Case "15"
RESULT = "Overhead"
Case "16"
RESULT = "Revolving"
Case "17"
RESULT = "Pass Thru"
Case "18"
RESULT = "Accordion"
Case "19"
RESULT = "Panel"
Case "20"
RESULT = "Communicating"
Case Else
RESULT = "Unknown Door Type"
End Select


If you had wanted the Formula property to return the raw value of the "type" property, you can still make use of "On Error Resume Next". The most efficient way is to set the RESULT to the default value up front, and then reset it only if the object is of a type for which the property value you are extracting exists. Using the door type as a number example from that previous article, Door/Window Assemblies could be accommodated by modifying the formula to the following:
On Error Resume Next

RESULT = -1

Set acadApp = GetObject(,"AutoCAD.Application")
Set doorObj = acadApp.ActiveDocument.ObjectIDToObject( [ObjectID] )
Set doorStyle = doorObj.Style
doorTypeInt = doorStyle.Type
If "[ObjectType]" = "Door" Then
RESULT = doorTypeInt
End If


You need to test for the ObjectType (which can be obtained through an automatic property source) at the end, since you only want to reset the RESULT value if the object is a Door. I chose -1 as the value to report for Door/Window Assembly objects since the Door type numbers start at 0 and increase from there; should a future release add one or more new door types, it is likely that those types would be assigned numbers starting with 21 and increasing, making -1 a relatively safe choice to indicate that the object is not a Door while still maintaining the same data type.

No comments: