C# code to create Wblock does not save it to 0,0,0

C# code to create Wblock does not save it to 0,0,0

user08QGULW
Explorer Explorer
1,765 Views
6 Replies
Message 1 of 7

C# code to create Wblock does not save it to 0,0,0

user08QGULW
Explorer
Explorer

 

        [CommandMethod("WW", CommandFlags.UsePickSet | CommandFlags.Redraw)]
        public void WW()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            doc.Editor.Command("._-OVERKILL", "_ALL", "", "");

            string message, title, defaultValue;
            string myValue;
            message = "Enter The First Six Digits" + Environment.NewLine + "of the Work Order Number";
            title = "Save W-Block";
            defaultValue = "";
            myValue = Interaction.InputBox(message, title, defaultValue);
            if (myValue == "") return;
            MyVar = myValue.Substring(0, 3);
            MyResult = myValue;
            MyFolder = @"W:\" + MyVar + "\\";

            var prRes = ed.GetSelection();
            if (prRes.Status != PromptStatus.OK)
                return;
            var objIds = new ObjectIdCollection();
            var objIdArray = prRes.Value.GetObjectIds();

            foreach (var id in objIdArray)
                objIds.Add(id);

            using (var newDb = new Database(true, false))
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    db.Wblock(newDb, objIds, new Point3d(0,0,0), DuplicateRecordCloning.Ignore);
                    FileName = MyFolder + MyResult;
                    FileName = FileName.Replace("(OK,", "");
                    FileName = FileName.Replace(")", "");
                    var mess = "Enter The Name to Save the File as";
                    var tle = "Save W-Block";

                    myValue = Interaction.InputBox(mess, tle, FileName);

                    if (myValue == "")
                    {
                        Application.Quit();
                    }
                    else if (myValue != "")
                    {
                        if (File.Exists(myValue + ".dwg"))
                        {
                            var dialogResult = MessageBox.Show("File Exists, Overwrite?", "Question",
                                MessageBoxButtons.YesNo);
                            if (dialogResult == DialogResult.Yes)
                            newDb.SaveAs(myValue + ".dwg", DwgVersion.AC1027);
                            else
                                return;
                        }
                        else
                        {
                            newDb.SaveAs(myValue + ".dwg", DwgVersion.AC1027);
                        }
                    }
                }
            }
            ;
            MoreWs();
        }

This is the method I'm using, but the Wblock is never at the 0,0,0 location. Any help would be appreciated. 

0 Likes
Accepted solutions (1)
1,766 Views
6 Replies
Replies (6)
Message 2 of 7

pbejse
Mentor
Mentor

You will have better luck posting this --> here

 

HTH

0 Likes
Message 3 of 7

lena.talkhina
Alumni
Alumni

Thank you for the head-up @pbejse !
I moved this thread to .NET forum

Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям! | Do you find the posts helpful? "LIKE" these posts!
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.



Лена Талхина/Lena Talkhina
Менеджер Сообщества - Русский/Community Manager - Russian

0 Likes
Message 4 of 7

norman.yuan
Mentor
Mentor

So, you want to select some entities and save it as a block dwg file with Database.WBlock() method. I assume when you say "...but the Wblock is never at the 0,0,0 location...", you mean the base point of the created block is not where you want it to be. But you do explicitly passed Point3d(0,0,0), so yes, the base point is where your code want it to be - the current drawing's 0.0, 0.0, which might not be the intended block insertion/base point, if the selected entities is far from 0.0, 0.0.

 

Since your intention is to create a block by selecting existing entities in current drawing, you SHOULD also ask user to select a point as the base/insertion point for the block. Following code works as expected, where I select a Circle and a few other entities to be WBlocked, then I select the center of the circle as base point. The generated block has its base point at where it should be:

 

        [CommandMethod("MyWBlock")]
        public static void DoWBlock()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;

            SelectEntitiesAndBasePoint(ed, out ObjectIdCollection entIds, out Point3d basePoint);
            if (entIds!=null)
            {
                using (var newDB = new Database(true, true))
                {
                    dwg.Database.Wblock(newDB, entIds, basePoint, DuplicateRecordCloning.Ignore);
                    newDB.SaveAs(@"E:\Temp\MyTestBlock.dwg", DwgVersion.Current);
                }
            }
        }


        private static void SelectEntitiesAndBasePoint(
            Editor ed, out ObjectIdCollection entIds, out Point3d basePoint)
        {
            entIds = null;
            basePoint = Point3d.Origin;

            var selectionRes = ed.GetSelection();
            if (selectionRes.Status== PromptStatus.OK)
            {
                var pointRes = ed.GetPoint("\nSelect base point for WBLOCKing:");
                if (pointRes.Status== PromptStatus.OK)
                {
                    entIds = new ObjectIdCollection(selectionRes.Value.GetObjectIds());
                    basePoint = pointRes.Value;
                }
            }
        }

 

Also regarding your code:

 

1. There is no need to start a Transaction for calling Database.WBlock(). Furthermore, you started a Transaction but not commit it, which is bad practice. Again, transaction is not needed here;

 

2. When creating the new Database, yes, the first argument should be "true",  but the second argument should also be "true" (meaning no document);

 

3. Why do you have this:

if (myValue == "")
                    {
                        Application.Quit();
                    }

You cannot (should not) close down AutoCAD while you are running a command in DocumentContext. In your case, if the block name (myValue) is not entered, you simply not save the newDB and let it be disposed at the end of using(){...} block, and the command finishes. Better yet, you should ask user for the block name BEFORE creating the newDB and call WBlock() method, so that if user does not enter block name, no new database is created, nor WBlock() call is needed.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 7

user08QGULW
Explorer
Explorer

Hi Norman,

Thank you for your time and suggestions. I will make the changes accordingly!

0 Likes
Message 6 of 7

user08QGULW
Explorer
Explorer

Hi Norman,

 

After testing the code you provided, the W-Block still maintains it's original position on the screen. I would like the W-Block to be created at the 0,0 location. Is this possible?

 

Thank you for your help!

Scott

0 Likes
Message 7 of 7

norman.yuan
Mentor
Mentor
Accepted solution

Well, I do not know how you tested. Following video shows how the code from my previous post worked as expected: I ran the code twice to select 2 different groups of entities, with different base points, to create the block file with WBlock() method. The generated block file were exactly as I wanted with their insertion points in the correct location. So, I have no other answer for you.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes