OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > CorelDRAW/Corel DESIGNER VBA

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 15-02-2003, 12:38
Superfreak
Guest
 
Posts: n/a
Default First Makro! First Error!

Hi Folks !

Now i got a Book VB for dummies and wrote a first small makro:

Sub neue1()

Dim size1, size2, size3, size4, color1, color2, color3, color4
Randomize
size1 = Int((20 * Rnd) + 1)
size2 = Int((20 * Rnd) + 1)
size3 = Int((20 * Rnd) + 1)
size4 = Int((20 * Rnd) + 1)
color1 = Int((100 * Rnd) + 1)
color2 = Int((100 * Rnd) + 1)
color3 = Int((100 * Rnd) + 1)
color4 = Int((20 * Rnd) + 1)
Dim s5 As Shape
Set s5 = ActiveLayer.CreateRectangle(size1, size2, size3, size4, 0, 0, 0, 0)
s5.Fill.UniformColor.CMYKAssign color1, color2, color3, color4

End Sub



It creates a rectangle with random size and color. It works, but sometimes
the error occures: The sides of the object cannot be 0.

What is my Mistake?
Reply With Quote
  #2  
Old 17-02-2003, 10:49
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,941
Blog Entries: 4
Default Re: First Makro! First Error!

Superfreak,

Your problem is the following. You used Rnd function to generate random numbers. It generates numbers in range between 0 and 1 (including zero but never 1). So when you do it like this: Int(20*Rnd + 1) what you actually are getting is integer numbers between 1 and 20.

CreateRectangle requires two bounding points (e.g. upper left and lower right corners) of the rectangle to create. If you specify, let's say, (1,2)-(3,4), then your rectangle will be 2" wide (3-1) and 2" high (4-2).

However the chances could be that "size1" and "size3" could be equal and/or "size2" and "size4". In this case, either width or height (or both) of the rectangle will be 0, and an error will occur.

I'm just curious if you really wanted to use integer numbers for coordinates in the first place. Second, you would probably be better off if you specify one point (e.g. lower left corner) and the size of the rectangle. In this case, it'd be better to use CreateRectangle2 function which does just that.

You can rewrite your example as follows:

Code:
Sub TestRandom()
    Dim x As Double, y As Double
    Dim sx As Double, sy As Double
    Dim color1 As Long, color2 As Long, color3 As Long, color4 As Long
    Dim s As Shape
    Dim n As Long
    
    Randomize
    For n = 1 To 100
        x = 20 * Rnd()
        y = 20 * Rnd()
        sx = 19.9 * Rnd() + 0.1
        sy = 19.9 * Rnd() + 0.1
        color1 = CLng(101 * Rnd())
        color2 = CLng(101 * Rnd())
        color3 = CLng(101 * Rnd())
        color4 = CLng(101 * Rnd())
        
        Set s = ActiveLayer.CreateRectangle2(x, y, sx, sy)
        s.Fill.UniformColor.CMYKAssign color1, color2, color3, color4
    Next n
End Sub
Note that I made a couple changes in your code. Now x,y are generated between 0" and 20" (double values, not integers) as well as sx and ay are between 0.1" and 20".

Also the color components are generated between 0 and 100 inclusive. I just had to multiply Rnd by 101 in order to achieve that. Because Rnd generates numbers that are ALWAYS less than 1 (but could be very close), after multiplying by 101 you'll get a maximum of something like 100.999 which will be converted to 100 when assigning it to a Long variable.

Your previous formula used to give values between 1 and 100 and never 0.

I also put a loop to generate 100 rectangles for test purposes. Feel free to remove it.

I hope this helps.
Reply With Quote
  #3  
Old 18-02-2003, 14:09
Superfreak
Guest
 
Posts: n/a
Default Thanks a lot

I see... and understand!!!!

Thanks for you enlightment!!

I will enhance it some more with a formular.


Attached Files
File Type: cdr Bart_Simpson.cdr (67.9 KB, 652 views)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
File Converter script error CorelDummy CorelDRAW/Corel DESIGNER VBA 2 25-01-2005 09:38
CorelDraw 8 error in DLL file - help !!! cannot open!!! Anonymous General 4 31-10-2004 16:52
Export filter compile error ddonnahoe CorelDRAW/Corel DESIGNER VBA 1 26-01-2004 10:15
"Unexpected condition" error message (with code) s_federici CorelDRAW/Corel DESIGNER VBA 10 10-07-2003 22:46
"Object too complex" error dcsquare CorelDRAW/Corel DESIGNER VBA 0 25-05-2003 11:13


All times are GMT -5. The time now is 22:40.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2023, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com