Questions about the system

Questions about the system

artur_dobriyan
Explorer Explorer
1,209 Views
4 Replies
Message 1 of 5

Questions about the system

artur_dobriyan
Explorer
Explorer

Hello. I am here trying to make a code where you put in data like width, length, how many and setera. Like in the photo.

And i wanted to 

1. Change line type and color, but option Set is not allowing me to make any it always glowing red in code.

2. So every cube had it own personal sequenced number but i have no formula for that so i wiil need to change my code.

3. I suppose AcadLine and other acad types are not working or not get registered.

Here is the code because i cannot upload dvb files.

Private Sub DrawRectangle_Click()

Dim starter, pt1, pt2, pt3, pt4, vectorup, bluestart, blueend, redstart, redend, name As Variant
Dim textObj As Variant
Dim i, j, cap1, cap2, distanceright, distanceup As Integer
Dim pi As Double
Dim blue, red As Variant

UserForm1.Hide
pi = 3.14
cap1 = Val(TextBox3.Text)
cap2 = Val(TextBox4.Text)

distanceright = Val(TextBox1.Text) + Val(TextBox5.Text)
distanceup = Val(TextBox2.Text) + Val(TextBox5.Text)
UserForm1.Hide
starter = ThisDrawing.Utility.GetPoint(, "Pick left starter corner")
vectorup = starter
For j = 1 To cap2
vectorup = ThisDrawing.Utility.PolarPoint(starter, pi / 2, distanceup * (j - 1))
For i = 1 To cap1
pt1 = ThisDrawing.Utility.PolarPoint(vectorup, 0, distanceright * (i - 1))
pt2 = ThisDrawing.Utility.PolarPoint(pt1, 0, Val(TextBox1.Text))
pt3 = ThisDrawing.Utility.PolarPoint(pt2, pi / 2, Val(TextBox2.Text))
pt4 = ThisDrawing.Utility.PolarPoint(pt1, pi / 2, Val(TextBox2.Text))

ThisDrawing.ModelSpace.AddLine pt1, pt2
ThisDrawing.ModelSpace.AddLine pt2, pt3
ThisDrawing.ModelSpace.AddLine pt3, pt4
ThisDrawing.ModelSpace.AddLine pt4, pt1

bluestart = ThisDrawing.Utility.PolarPoint(pt1, pi / 4, Sqr((Val(TextBox1.Text) / 4) ^ 2 + (Val(TextBox2.Text) / 10) ^ 2))
blueend = ThisDrawing.Utility.PolarPoint(bluestart, pi / 2, Val(TextBox2.Text) * 9 / 10 + Val(TextBox5.Text) * 6 / 10)
ThisDrawing.ModelSpace.AddLine bluestart, blueend

redstart = ThisDrawing.Utility.PolarPoint(pt3, 5 * pi / 4, Sqr((Val(TextBox1.Text) / 4) ^ 2 + (Val(TextBox2.Text) / 10) ^ 2))
redend = ThisDrawing.Utility.PolarPoint(redstart, pi / 2, Val(TextBox2.Text) / 10 + Val(TextBox5.Text) * 4 / 10)
ThisDrawing.ModelSpace.AddLine redstart, redend

name = ThisDrawing.Utility.PolarPoint(pt1, pi / 4, Sqr((Val(TextBox1.Text) / 2) ^ 2 + (Val(TextBox2.Text) / 2) ^ 2))
ThisDrawing.ModelSpace.AddText TextBox8.Text & Val(TextBox9.Text) + i + j - 2, name, Val(TextBox2.Text) / 10


Next i
Next j

End Sub

0 Likes
Accepted solutions (1)
1,210 Views
4 Replies
Replies (4)
Message 2 of 5

saboh12617
Contributor
Contributor
Accepted solution

Hi,

 

I am a little confused about what you want to achieve and your code as using the generic names for your textboxes is not helping understanding the code.

 

However I managed to get it to work. If you want to color your Lines you have to do it in two times:

1. creating the line as you did

2. changing its color

For 2. to be doable you need the code to know which line to color, ie. put the line into a named variable.

Also the colors in AutoCAD VBA are objects of type "TrueColor" . So you need to use it as indicated in the code example of the help linked.

 

Let's suppose you want the "blue" line of your code to be blue. You can do it like this:

 

Dim colorBlue As AcadAcCmColor
Set colorBlue = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor." & Left(AcadApplication.Version, 2))
' Defining the RGB values
colorBlue.SetRGB 0, 0, 180


bluestart = ThisDrawing.Utility.PolarPoint(pt1, pi / 4, Sqr((Val(TextBox1.Text) / 4) ^ 2 + (Val(TextBox2.Text) / 10) ^ 2))
blueend = ThisDrawing.Utility.PolarPoint(bluestart, pi / 2, Val(TextBox2.Text) * 9 / 10 + Val(TextBox5.Text) * 6 / 10)
' assigning the line object to a variable
Dim blueLine as AcadLine
Set blueLine = ThisDrawing.ModelSpace.AddLine(bluestart, blueend)
' changing the line color
blueLine.TrueColor = colorBlue

 

This should work the same way for the Red line.

 

This answers I suppose your points 1. and 3., but for the 2. I did not understand what you mean. If you want to group your objects "in the code", i suggest you to look for SelectionSets. If you want to group them in the drawing you can create a group as you would do Manually. You can also make a parametric bloc.

 

Finally some hints to better understanding VBA:

Be careful when you write

 

Dim x, y, z As Integer

 

Only z gets the Integer type, x and y are Variant

You should write

 

Dim x As Integer, y As Integer, z As Integer

 

In the UserForm editor you can change the name of the TextBoxes so they are easier to understand.

For example, instead of writing 

 

 

cap1 = Val(TextBox3.Text)

 

You could write
 

 

cap1 = Val(BoxCap1.Text)

 

And about getting the TextBox value, i think you could go with

 

Val(TextBox.Value)

 

0 Likes
Message 3 of 5

saboh12617
Contributor
Contributor

Here is the whole code indented and commented a little

Private Sub DrawRectangle_Click()
    ' Variant points <=> Array(x, y, z)
    Dim starter, pt1, pt2, pt3, pt4, vectorup, bluestart, blueend, redstart, redend, name

    ' not used: Dim textObj

    Dim colorBlue As AcadAcCmColor
    Set colorBlue = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor." & Left(AcadApplication.Version, 2))
    ' Defining the blue color RGB values
    colorBlue.SetRGB 0, 180, 0
    Dim colorRed As AcadAcCmColor
    Set colorRed = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor." & Left(AcadApplication.Version, 2))
    ' Defining the red color RGB values
    colorRed.SetRGB 255, 0, 0

    ' hiding the userform
    UserForm1.Hide

    Dim pi As Double
    pi = 3.14

    Dim cap1 As Integer, cap2 As Integer
    cap1 = Val(TextBox3.Text)
    cap2 = Val(TextBox4.Text)
    
    Dim distanceright As Integer, distanceup As Integer
    distanceright = Val(TextBox1.Text) + Val(TextBox5.Text)
    distanceup = Val(TextBox2.Text) + Val(TextBox5.Text)

    ' you already hid the userform before: UserForm1.Hide

    starter = ThisDrawing.Utility.GetPoint(, "Pick left starter corner")
    vectorup = starter

    Dim i As Integer, j As Integer
    For j = 1 To cap2
        vectorup = ThisDrawing.Utility.PolarPoint(starter, pi / 2, distanceup * (j - 1))
        For i = 1 To cap1
            pt1 = ThisDrawing.Utility.PolarPoint(vectorup, 0, distanceright * (i - 1))
            pt2 = ThisDrawing.Utility.PolarPoint(pt1, 0, Val(TextBox1.Text))
            pt3 = ThisDrawing.Utility.PolarPoint(pt2, pi / 2, Val(TextBox2.Text))
            pt4 = ThisDrawing.Utility.PolarPoint(pt1, pi / 2, Val(TextBox2.Text))
            
            ThisDrawing.ModelSpace.AddLine pt1, pt2
            ThisDrawing.ModelSpace.AddLine pt2, pt3
            ThisDrawing.ModelSpace.AddLine pt3, pt4
            ThisDrawing.ModelSpace.AddLine pt4, pt1
            
            
            bluestart = ThisDrawing.Utility.PolarPoint(pt1, pi / 4, Sqr((Val(TextBox1.Text) / 4) ^ 2 + (Val(TextBox2.Text) / 10) ^ 2))
            blueend = ThisDrawing.Utility.PolarPoint(bluestart, pi / 2, Val(TextBox2.Text) * 9 / 10 + Val(TextBox5.Text) * 6 / 10)
            ' assigning the line object to a variable
            Dim blueLine as AcadLine
            Set blueLine = ThisDrawing.ModelSpace.AddLine(bluestart, blueend)
            ' changing the line color
            blueLine.TrueColor = colorBlue

            redstart = ThisDrawing.Utility.PolarPoint(pt3, 5 * pi / 4, Sqr((Val(TextBox1.Text) / 4) ^ 2 + (Val(TextBox2.Text) / 10) ^ 2))
            redend = ThisDrawing.Utility.PolarPoint(redstart, pi / 2, Val(TextBox2.Text) / 10 + Val(TextBox5.Text) * 4 / 10)
            Dim redLine as AcadLine
            Set redLine = ThisDrawing.ModelSpace.AddLine(redstart, redend)
            ' changing the line color
            blueLine.TrueColor = colorRed

            name = ThisDrawing.Utility.PolarPoint(pt1, pi / 4, Sqr((Val(TextBox1.Text) / 2) ^ 2 + (Val(TextBox2.Text) / 2) ^ 2))
            ThisDrawing.ModelSpace.AddText TextBox8.Text & Val(TextBox9.Text) + i + j - 2, name, Val(TextBox2.Text) / 10
            
        Next i
    Next j
    
End Sub
0 Likes
Message 4 of 5

artur_dobriyan
Explorer
Explorer

Many thanks for your answer.

I have managed to make Line color and type work yesterday. Didnt knew that Set function requires to use () in the value section.

Havent known that you are required to write every variable as individual integer, fought its like in the C or python where it works for every in the line.

What about textboxes i knew i can do it, but fought it would be easier for me because i often misspritn text and after that finding the error would be a nightmare.

What about 2 question. I just made n=n+1 formula in the for so it always gets a new number for the box.

Again many thanks for your answer it improved me a little bit.

0 Likes
Message 5 of 5

saboh12617
Contributor
Contributor

Yes, indeed VBA syntax is very tricky and confusing.

Generally speaking when you call a function without using its return value you should not put the parenthesis, as you did for

ThisDrawing.ModelSpace.AddLine pt1, pt2

When you apply its return value to a variable, then you need the parenthesis. As you did for

pt2 = acaddoc.Utility.PolarPoint(pt1, 0, TextBox)

And yes, the Dim statement is just different (many people get confused about it).

Glad you made it work!

0 Likes