Draws grid of rectangles faster C#

Former Member
Former Member

The Starting Code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
    [CgsAddInMacro]
        public void Grid(int cols = 100, int rows = 100)
        {
            double posX = 0;
            double posY = 0;
            double w = 1;
            double h = 1;
            for (int i = 0; i < cols; i++)
            {
                for (int k = 0; k < rows; k++)
                {
                    corelApp.ActiveLayer.CreateRectangle2(posX, posY, w, h);
                    posX += w;
                }
                posY -= h;
                posX = 0;
            }
        }

Actual Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  [CgsAddInMacro]
        public void FastGrid(int cols = 100, int rows = 100)
        {
            try
            {
               
                Shape sample = corelApp.ActiveVirtualLayer.CreateRectangle2(0, 0, 1, 1);
                ShapeRange grid = corelApp.CreateShapeRange();
              
                int mShapes = cols * rows;

                grid.Add(sample.TreeNode.GetCopy().VirtualShape);

                ShapeRange tempShape = corelApp.CreateShapeRange();
                while (grid.Count < cols)
                {
                    if (grid.Count * 2 > cols / 2 && grid.Count + tempShape.Count < cols)
                    {
                        grid.AddRange(tempShape.Duplicate(grid.SizeWidth, 0));
                    }
                    else if(grid.Count * 2 < cols)
                    {
                        grid.AddRange(grid.Duplicate(grid.SizeWidth, 0));
                        tempShape.AddRange(grid);
                    }
                    else
                    {
                        grid.Add(sample.Duplicate(grid.SizeWidth));
                    }
                 
                }
                ShapeRange line = corelApp.CreateShapeRange();
                line.AddRange(grid);
                while (grid.Count < mShapes)
                {

                  
                    if (grid.Count * 2 > mShapes / 2 && grid.Count + tempShape.Count < mShapes)
                    {
                        grid.AddRange(tempShape.Duplicate(0, grid.SizeHeight));
                    }
                    else if (grid.Count * 2 < mShapes)
                    { 
                        grid.AddRange(grid.Duplicate(0, grid.SizeHeight));
                        tempShape.AddRange(grid);

                    }
                    else
                    {
                        grid.AddRange(line.Duplicate(0, grid.SizeHeight));
                    }
                }
                grid.MoveToLayer(corelApp.ActiveLayer);
                MessageBox.Show(grid.Count.ToString());
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

But i need more speed

Parents
No Data
Reply Children