示例#1
0
        public static void EditQR()
        {
            Document doc =
                Application.DocumentManager.MdiActiveDocument;
            Editor   ed = doc.Editor;
            Database db = doc.Database;

            // Ask user to select an QR code, hatch or raster image

            PromptEntityOptions peo =
                new PromptEntityOptions("Select a QR code: ");

            peo.SetRejectMessage(
                "\nMust be a hatch or a raster image"
                );
            peo.AddAllowedClass(typeof(Hatch), true);

            // AutoCAD crash if we try AddAllowedClass for RasterImage
            // when no raster image is defined or just hatch QRs were
            // defined, probably because in C++ we need to call
            // acedArxLoad("acismui.arx"), which is not exposed in .NET,
            // so let's check before if the drawing contains any
            // RasterImages, if not we don't need this filter.

            if (!RasterImageDef.GetImageDictionary(db).IsNull)
            {
                peo.AddAllowedClass(typeof(RasterImage), true);
            }
            PromptEntityResult entityResult = ed.GetEntity(peo);

            if (entityResult.Status != PromptStatus.OK)
            {
                return;
            }

            Transaction tr = db.TransactionManager.StartTransaction();

            using (tr)
            {
                Entity ent =
                    tr.GetObject(entityResult.ObjectId, OpenMode.ForRead)
                    as Entity;
                ResultBuffer rb =
                    ent.GetXDataForApplication(APPLICATION_PREFIX);

                if (rb != null && rb.AsArray().Length == 0)
                {
                    ed.WriteMessage("\nThis is not a valid QR code");
                    tr.Commit(); //faster
                    return;
                }

                // Show the form with current information

                QRCodeForm form = new QRCodeForm();
                form.IsEditing = true;
                form.QREncodeDataAsResultBuffer = rb;
                rb.Dispose();
                System.Windows.Forms.DialogResult res =
                    Application.ShowModalDialog(form);
                if (res != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }

                //Get insert point and size

                double size =
                    ent.GeometricExtents.MaxPoint.X -
                    ent.GeometricExtents.MinPoint.X;
                Point3d inspt = ent.GeometricExtents.MinPoint;


                if (ent is RasterImage)
                {
                    // Just update the raster image definition

                    RasterImage    image    = ent as RasterImage;
                    RasterImageDef imageDef =
                        tr.GetObject(image.ImageDefId, OpenMode.ForWrite)
                        as RasterImageDef;
                    imageDef.SourceFileName =
                        FormatDataHelper.EncodeQrCodeUrl(form.QREncodeData);
                    imageDef.Load();
                }
                else
                {
                    // Erase current entity

                    ent.UpgradeOpen();
                    ent.Erase();

                    // Create a new one

                    Entity newEnt =
                        GenerateQRHatch(
                            form.QREncodeData, form.QREncode,
                            form.QRVersion, form.QRErrorCorrect, (int)size
                            );
                    if (newEnt == null)
                    {
                        return;
                    }
                    ResultBuffer newRb = form.QREncodeDataAsResultBuffer;
                    newEnt.XData = newRb;
                    newRb.Dispose();
                    newEnt.TransformBy(
                        Matrix3d.Displacement(inspt.GetAsVector())
                        );
                    AppendEntityToCurrentSpace(newEnt);
                }

                tr.Commit();
            }
        }
示例#2
0
        public static void GenerateQRByForm()
        {
            // Show the form
            QRCodeForm form = new QRCodeForm();

            System.Windows.Forms.DialogResult res =
                Application.ShowModalDialog(form);
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            // Obtain insert point and size

            Point3d insertPoint = Point3d.Origin;
            double  size        = form.QRSize;

            if (
                PromptInsertPointAndSize(
                    form.QRSacelOnScreen,
                    ref insertPoint, ref size) != PromptStatus.OK
                )
            {
                return;
            }

            // Generate the QR entity

            Entity ent = null;

            switch (form.QREntityType)
            {
            case QRCodeForm.QRType.Hatch:

                // Generate the QR as a Hatch entity

                ent = GenerateQRHatch(
                    form.QREncodeData, form.QREncode,
                    form.QRVersion, form.QRErrorCorrect, size
                    );
                if (ent == null)
                {
                    return;
                }

                // Append to the current space

                AppendEntityToCurrentSpace(ent);
                break;

            case QRCodeForm.QRType.Online:

                // Generate the QR as an ONline Raster Image entity

                string uri =
                    FormatDataHelper.EncodeQrCodeUrl(form.QREncodeData);
                ent = CreateRasterImage(uri, size);
                break;
            }

            if (ent == null)
            {
                return;
            }

            // Add the xdata and move the ent to the correct location

            SetXDataAndMoveToLocation(
                ent, form.QREncodeDataAsResultBuffer, insertPoint
                );
        }