Getting Starting in VBA 2
Posted: Thu Aug 02, 2001 11:39 pm
Well here we are back again and I hope you have fully understood my Getting Starting in VBA.
Now for some serious stuff:
First we will create a tool to draw rectangles – hang on! - what if we only want to draw three sides or two sides, our tool would need to cater for this – Here it is :
Public Sub DrawRectangle(DRLLPnt As Point, DRXLen As Double, DRYLen As Double, _ DRLayer As String, DRColor As Integer, DRBottom As Boolean, DRTop As Boolean, _ DRRight As Boolean, DRLeft As Boolean)
' This routine will create a rectangle of lines with the designated layer and color
On Error Resume Next
' It will make the layer if it does not exist using Tools.MakeLayer
Tools.MakeLayer DRLayer, DRColor, "Continuous"
' Dimension the other three point around the rectangle
Dim DRLRPnt As Point
Dim DRULPnt As Point
Dim DRURPnt As Point
' Calculate the other three points around the rectangle
Set DRLRPnt = IntelliCAD.Library.CreatePoint(DRLLPnt.x + DRXLen, DRLLPnt.y, 0)
Set DRULPnt = IntelliCAD.Library.CreatePoint(DRLLPnt.x, DRLLPnt.y + DRYLen, 0)
Set DRURPnt = IntelliCAD.Library.CreatePoint(DRULPnt.x + DRXLen, DRULPnt.y, 0)
'Dimension the line object
Dim objDRLine As IntelliCAD.Line
' Now draw the lines using the start and end points that we calculated and set the layer and color
' Bottom Line
If DRBottom = True Then
Set objDRLine = IntelliCAD.ActiveDocument.ModelSpace.AddLine(DRLLPnt, DRLRPnt)
With objDRLine
.Layer = DRLayer
.Color = vicByLayer
.Update
End With
End If
' Left Line
If DRLeft = True Then
Set objDRLine = IntelliCAD.ActiveDocument.ModelSpace.AddLine(DRLLPnt, DRULPnt)
With objDRLine
.Layer = DRLayer
.Color = vicByLayer
.Update
End With
End If
' Right Line
If DRRight = True Then
Set objDRLine = IntelliCAD.ActiveDocument.ModelSpace.AddLine(DRLRPnt, DRURPnt)
With objDRLine
.Layer = DRLayer
.Color = vicByLayer
.Update
End With
End If
' Top Line
If DRTop = True Then
Set objDRLine = IntelliCAD.ActiveDocument.ModelSpace.AddLine(DRULPnt, DRURPnt)
With objDRLine
.Layer = DRLayer
.Color = vicByLayer
.Update
End With
End If
End Sub
Now lets look at this a little closer “Public Sub DrawRectangle ( with variable stuff in here )” should be no problems – BUT – what is a Boolean?
Well, it's just like an answer that is either True or False that you can set or test. It really comes from the Boolean Truth Tables that programmers use – more on this later.
Notice we have used a _ at the end of each line between the parenthesis, it just tells VBA there’s more on the next line to read.
The Error trapping is simple – and has been previously covered – OK – I won’t bring it up again!
Hang on – What! we are using Tools within Tools? – YES – no need to reinvent the wheel and this gives us certainty that this part of code has been tested.
After Dim we create the other point around the rectangle using ICAD’s Library CreatePoints object that we need an x, y, and z – well we can do without the z but I just put in it anyway.
Even though we are drawing 4 lines I have only Dim 1 line object because we can reuse it.
Now we bring in the Boolean and if it is set to true the line will be drawn so you can see we can set any one of these to draw or not and either create a u shape if we like or any other combination.
We already know how to add a circle and a line is not much different only use the AddLine method.
What’s with the With? – well you can use with objectname and .Property = ??? to save you retyping the objectname over and over.
Next installment I will show you how to use the tool.
------------------
Regards
John Finlay
[This message has been edited by John Finlay (edited 08-03-2001).]
Now for some serious stuff:
First we will create a tool to draw rectangles – hang on! - what if we only want to draw three sides or two sides, our tool would need to cater for this – Here it is :
Public Sub DrawRectangle(DRLLPnt As Point, DRXLen As Double, DRYLen As Double, _ DRLayer As String, DRColor As Integer, DRBottom As Boolean, DRTop As Boolean, _ DRRight As Boolean, DRLeft As Boolean)
' This routine will create a rectangle of lines with the designated layer and color
On Error Resume Next
' It will make the layer if it does not exist using Tools.MakeLayer
Tools.MakeLayer DRLayer, DRColor, "Continuous"
' Dimension the other three point around the rectangle
Dim DRLRPnt As Point
Dim DRULPnt As Point
Dim DRURPnt As Point
' Calculate the other three points around the rectangle
Set DRLRPnt = IntelliCAD.Library.CreatePoint(DRLLPnt.x + DRXLen, DRLLPnt.y, 0)
Set DRULPnt = IntelliCAD.Library.CreatePoint(DRLLPnt.x, DRLLPnt.y + DRYLen, 0)
Set DRURPnt = IntelliCAD.Library.CreatePoint(DRULPnt.x + DRXLen, DRULPnt.y, 0)
'Dimension the line object
Dim objDRLine As IntelliCAD.Line
' Now draw the lines using the start and end points that we calculated and set the layer and color
' Bottom Line
If DRBottom = True Then
Set objDRLine = IntelliCAD.ActiveDocument.ModelSpace.AddLine(DRLLPnt, DRLRPnt)
With objDRLine
.Layer = DRLayer
.Color = vicByLayer
.Update
End With
End If
' Left Line
If DRLeft = True Then
Set objDRLine = IntelliCAD.ActiveDocument.ModelSpace.AddLine(DRLLPnt, DRULPnt)
With objDRLine
.Layer = DRLayer
.Color = vicByLayer
.Update
End With
End If
' Right Line
If DRRight = True Then
Set objDRLine = IntelliCAD.ActiveDocument.ModelSpace.AddLine(DRLRPnt, DRURPnt)
With objDRLine
.Layer = DRLayer
.Color = vicByLayer
.Update
End With
End If
' Top Line
If DRTop = True Then
Set objDRLine = IntelliCAD.ActiveDocument.ModelSpace.AddLine(DRULPnt, DRURPnt)
With objDRLine
.Layer = DRLayer
.Color = vicByLayer
.Update
End With
End If
End Sub
Now lets look at this a little closer “Public Sub DrawRectangle ( with variable stuff in here )” should be no problems – BUT – what is a Boolean?
Well, it's just like an answer that is either True or False that you can set or test. It really comes from the Boolean Truth Tables that programmers use – more on this later.
Notice we have used a _ at the end of each line between the parenthesis, it just tells VBA there’s more on the next line to read.
The Error trapping is simple – and has been previously covered – OK – I won’t bring it up again!
Hang on – What! we are using Tools within Tools? – YES – no need to reinvent the wheel and this gives us certainty that this part of code has been tested.
After Dim we create the other point around the rectangle using ICAD’s Library CreatePoints object that we need an x, y, and z – well we can do without the z but I just put in it anyway.
Even though we are drawing 4 lines I have only Dim 1 line object because we can reuse it.
Now we bring in the Boolean and if it is set to true the line will be drawn so you can see we can set any one of these to draw or not and either create a u shape if we like or any other combination.
We already know how to add a circle and a line is not much different only use the AddLine method.
What’s with the With? – well you can use with objectname and .Property = ??? to save you retyping the objectname over and over.
Next installment I will show you how to use the tool.
------------------
Regards
John Finlay
[This message has been edited by John Finlay (edited 08-03-2001).]




