How to transform a CADCurvePolygon

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
Anderson
Posts: 13
Joined: 04 Dec 2008, 12:55

How to transform a CADCurvePolygon

Post by Anderson » 12 Mar 2009, 06:27

Dear Sergey,

I want to change the CADCurvePolygon entity's scale & rotation.
Could you show me some sample code to transform the CADCurvePolygon entity ?

Thanks!

Anderson

support
Posts: 3272
Joined: 30 Mar 2005, 11:36
Contact:

Re: How to transform a CADCurvePolygon

Post by support » 12 Mar 2009, 15:58

Dear Mr. Anderson,

The following code works with CAD Import .NET v.7 Beta. Please contact us via e-mail to support@cadsofttools.com accordingly the question of getting updates.

Code: Select all

        private void button1_Click(object sender, EventArgs e)
        {
            cadImage = new CADImage();
            this.cadImage.InitialNewImage();
            this.cadImage.UseDoubleBuffering = false;

            if (notAdded)
            {
                CADCollection<DPoint> points = new CADCollection<DPoint>();
                points.Add(new DPoint(0.1, 0.5, 0));
                points.Add(new DPoint(0.6, 1.1, 0));
                points.Add(new DPoint(0.4, 2.0, 0));
                CreatePolyline(points);
                CADHatch h = CreateHatch("ANSI31", points);

                CADHatch h1 = new CADHatch();
                h1.AssignEntity(h);
                CADMatrix m = CreateTransform(new DPoint(2, 2, 2), new DPoint(2, 0, 0), 90);
                TransformHatch(h1, m);
                h1.Loaded(this.cadImage.Converter);

                this.cadImage.CurrentLayout.Entities.Add(h1);
                this.cadImage.GetExtents();
                notAdded = false;
                this.cadPictBox.Invalidate();
            }
        }
        
        bool notAdded = true;

        private CADPolyLine CreatePolyline(CADCollection<DPoint> list)
        {
            CADPolyLine pl = new CADPolyLine();
            for (int i = 0; i < list.Count; i++)
            {
                CADVertex v = new CADVertex();
                v.Point = list[i];
                pl.AddEntity(v);
            }
            pl.Color = Color.Blue;
            pl.LineWeight = 1;
            pl.Closed = true;
            if (this.cadImage != null && this.cadImage.Converter != null)
                pl.Loaded(this.cadImage.Converter);
            this.cadImage.CurrentLayout.Entities.Add(pl);
            this.cadImage.Converter.OnCreate(pl);
            this.cadImage.GetExtents();
            return pl;
        }

        private CADHatch CreateHatch(string hatchName, CADCollection<DPoint> list)
        {

            CADHatch Hatch = new CADHatch();

            Hatch.HatchName = hatchName;
            CAD2DBoundaryList v2DBList = new CAD2DBoundaryList();
            v2DBList.BoundaryType = 7;// Polyline type
            Hatch.BoundaryData.Add(v2DBList);
            CAD2DPolyline v2DPolyline = new CAD2DPolyline();
            v2DBList.Add(v2DPolyline);
            for (int i = 0; i < list.Count; i++)
            {
                v2DPolyline.Vertexes.Add(new CAD2DPoint(list[i].X, list[i].Y));
            }
            v2DPolyline.Closed = true;
            Hatch.Color = Color.Yellow;
            HatchPatternData vHPData = new HatchPatternData();

            vHPData.baseP = new DPoint(0, 0, 0);
            vHPData.offset = new DPoint(0.1, 0.1, 0);
            vHPData.lineAngle = 35.0f;
            vHPData.isDash = false;
            vHPData.lines = null;
            vHPData.dashNum = 0;
            Hatch.HatchPatternData.Add(vHPData);
            Hatch.Loaded(this.cadImage.Converter);
            this.cadImage.CurrentLayout.Entities.Add(Hatch);
            this.cadImage.Converter.OnCreate(Hatch);
            this.cadImage.GetExtents();
            return Hatch;
        }

        private CADMatrix CreateTransform(DPoint scale, DPoint offset, double angle)
        {
            CADMatrix matrix = new CADMatrix();
            CADMatrix rotmatrix = new CADMatrix();
            rotmatrix.IdentityMat();
            matrix.IdentityMat();
            double a = Math.PI * angle / 180;
            double S = Math.Sin(a);
            double C = Math.Cos(a);
            rotmatrix.data[0, 0] = C;
            rotmatrix.data[1, 1] = C;
            rotmatrix.data[0, 1] = S;
            rotmatrix.data[1, 0] = -S;
            matrix = CADMatrix.MatXMat(matrix, rotmatrix);
            matrix = CADMatrix.MatXMat(matrix, CADMatrix.StdMat(scale, offset));
            return matrix;
        }

        private void TransformHatch(CADHatch hatch, CADMatrix matrix)
        {
            for (int i = 0; i < hatch.BoundaryData.Count; i++)
            {
                CAD2DBoundaryList v2DBList = hatch.BoundaryData[i] as CAD2DBoundaryList;
                for (int j = 0; j < v2DBList.Count; j++)
                {
                    CAD2DPolyline v2DPolyline = v2DBList[i] as CAD2DPolyline;
                    for (int k = v2DPolyline.Vertexes.Count - 1; k >= 0; k--)
                    {
                        CAD2DPoint pt = (CAD2DPoint)v2DPolyline.Vertexes[k];
                        DPoint p = matrix.PtXMat(pt);
                        v2DPolyline.Vertexes.RemoveAt(k);
                        v2DPolyline.Vertexes.Add(new CAD2DPoint(p.X, p.Y));
                    }
                }
            }
        }
Sergey.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Post Reply