5

I need to insert an external DWG into an AutoCAD drawing via C# plugin. I need to "ask" to the user the insertion point and rotation of the inserted block. Until now I've always used a lisp function that calls the command "._-insert" which gives a thumbnail of the block under the mouse, allows the user to click into the drawing to set the insertion point and from that point allows the user to click one more time to set the rotation. Now I want to avoid the use of Lisp or the use of low level API of AutoCAD because I need a solution that runs over various CAD environments. What I found is something like this:

public static void InsertDwg(string dwgName)
    {
        CADAPI.ApplicationServices.Document doc = CADAPI.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
        CADDB.Database db = doc.Database;
        CADAPI.EditorInput.Editor ed = doc.Editor;
        CADDB.ObjectId ObjId;
        using (CADDB.Transaction trx = db.TransactionManager.StartTransaction())
        {
            CADDB.BlockTable bt = db.BlockTableId.GetObject(CADDB.OpenMode.ForRead) as CADDB.BlockTable;
            CADDB.BlockTableRecord btrMs = bt[CADDB.BlockTableRecord.ModelSpace].GetObject(CADDB.OpenMode.ForWrite) as CADDB.BlockTableRecord;
            using (CADDB.Database dbInsert = new CADDB.Database(false, true))
            {
                dbInsert.ReadDwgFile(dwgName, CADDB.FileOpenMode.OpenForReadAndAllShare, true, string.Empty);
                ObjId = db.Insert(Path.GetFileNameWithoutExtension(dwgName), dbInsert, true);
            }
            CADAPI.EditorInput.PromptPointOptions ppo = new CADAPI.EditorInput.PromptPointOptions("\nInsertion Point");
            CADAPI.EditorInput.PromptAngleOptions ppa = new CADAPI.EditorInput.PromptAngleOptions("\nInsert Rotation");
            CADAPI.EditorInput.PromptPointResult ppr;
            ppr = ed.GetPoint(ppo);
            CADAPI.EditorInput.PromptDoubleResult ppd = ed.GetAngle(ppa);
            if (ppr.Status == CADAPI.EditorInput.PromptStatus.OK)
            {
                CADGEOM.Point3d insertPt = ppr.Value;
                CADDB.BlockReference bref = new CADDB.BlockReference(insertPt, ObjId);
                btrMs.AppendEntity(bref);
                trx.AddNewlyCreatedDBObject(bref, true);
                trx.Commit();
            }
        }
    }

But here I have two problems: The main one is that there is no preview under the mouse. The second is that the user needs to click 3 times instead of 2 to set both the insertion point and the rotation.

Is there any way that doesn't use some kind of SendCommand and does all of this stuff? TIA

Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69

2 Answers2

3

It seems Jigging is the way to go to allow the preview. I have three links for you.

The first is an example of creating a simple jig with polylines - you could extend this to a block.

The second link is similar but applies rotation to the mix. This is applied to a rectangle but again could be modified to accomodate a block.

The third link describes a different method - AutoCADs transient graphics interface. You must be using AutoCAD 2009 or later to use this method.

The last two links are from the Through the Interface blog, where you may find some more examples and is a very good starting point if you have problems, especially for coding C#.

JayP
  • 809
  • 7
  • 9
  • Thank you, from a simple overview it seems to solve the problem, but I can't use this solution because other CAD environments (specifically BricsCAD) doesn't have Jig logic. I was wondering an unique solution for both the environments but now I think that probably it's impossible to have it avoiding SendCommands. Anyway Thank you for your suggestion. – Tobia Zambon Feb 29 '12 at 14:20
0

You will want to use the AcEdJig class. It provides the preview. You will have to write the code to collect the insert point and rotation and to transform the block accordingly.

Here is the first link from my google search for example usage code.

Rodney Schuler
  • 2,158
  • 4
  • 23
  • 34