Increment variables in a For Each loop

Increment variables in a For Each loop

Anonymous
Not applicable
1,424 Views
1 Reply
Message 1 of 2

Increment variables in a For Each loop

Anonymous
Not applicable

I am having Trouble Incrementing a Variable inside a loop 

 

Here is my code. 

 

Every loop I want to iterate through the Range and subtract from my radius

 

Any Help is welcome

 

  For Each cell In DataRange2.Cells
    ReDim Preserve myArray(x)
    myArray(x) = cell.Value
    centerSet = myArray(x)
    Dim dotRad1 as Variant  
         
                center(0) = newSet: center(1) = centerSet: center(2) = 0
        
                ' Define the hatch
        
                patternName = "solid"
        
                PatternType = 0
        
                bAssociativity = True
        
                ' Create the associative Hatch object
        
                Set hatchObj = ThisDrawing.ModelSpace.AddHatch(PatternType, patternName, bAssociativity)
        
                ' Create the outer boundary for the hatch. (a circle)
        
                center(0) = newSet: center(1) = centerSet: center(2) = 0
        
                radius = dotrad1
        
        
                Set outerLoop(0) = ThisDrawing.ModelSpace.AddCircle(center, radius)
        
                ' Append the outer boundary to the hatch
                ' object, and display the hatch
        
                hatchObj.AppendOuterLoop (outerLoop)
        
                hatchObj.Evaluate
        
                ThisDrawing.Regen True
        
                hatchObj.Update

            
            dotRad1 = dotRad1 - ("Some Value Dosent matter")
            "Here at the dotRad1 var I would like to subtract from the var and sent the new value back to the top of the loop "
    Next cell
0 Likes
Accepted solutions (1)
1,425 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor
Accepted solution

I am not sure I know exactly the final result your code generates, except knowing it creates hatches according to data (from Excel sheet?).

 

It seems to me your question is rather easy to answer: simply declare variable "dotRad1" outside (before) the "For...Next" loop would do:

 

Dim dotRad1 As Double

' Give it a proper initial value. You do not want to create an outloop

' circle with radius=0.0 for the first hatch

dotRad1=100#  

For ....

    '' Creating a hatch

    '' incrementing "dotRad1" value

    dotRadi = dotRad1 - [whatever] ''Make sure it is not <= 0.0, if the incrementing is negative.

Next

 

Also, you might also want to move ThisDrawing.Regen() outside the For...Next loop (after the loop), so that the drawing only Regen once after all hatches created. Each regen in the loop takes time, slows down the code execution significantly, especially when the execution is called from outside AutoCAD (Excel, I guess).

Norman Yuan

Drive CAD With Code

EESignature

0 Likes