.Net eNotApplicable

This widget could not be displayed.

.Net eNotApplicable

Anonymous
Not applicable

 

I've been trying to test the following code.... and it fails when it gets to this "sr = ed.SelectAll(filter)". I think it is because I am opening a Document using the document manager. I've tested the SelectionSet code in another method that works with the Application.DocumentManager.MdiActiveDocument by first opening a drawing in AutoCAD that I know has blocks in it. It works fine, just seems to bomb out when I open a file using DOcumentManage. If I look at the file name for the MdiActiveDocument it isn't the one I opened. I have to have a file open already to be able to use NETLOAD and I suspect that because I issue the command for the code in that document that I need to some how tell the application that I want the newly opened file to be the active document. Just don't know how.

 

                DocumentCollection acDocMgr = Application.DocumentManager;
                Document targetDoc = acDocMgr.Open(f, false);
                Database db = targetDoc.Database;
                Editor ed = targetDoc.Editor;
                TypedValue[] tvl = new TypedValue[] { new TypedValue((int)DxfCode.Start, "INSERT") };
                SelectionFilter filter = new SelectionFilter(tvl);
                PromptSelectionResult sr;
                SelectionSet ss;

                try
                {
                    ObjectId[] idArray = null;
                    
                    sr = ed.SelectAll(filter); // < Failes here with eNotApplicable
                    ss = sr.Value;
                    idArray = ss.GetObjectIds();
                    using (Transaction trans = db.TransactionManager.StartTransaction())
                    {
                        foreach (ObjectId id in idArray)
                        {
                            BlockReference blkRef = trans.GetObject(id, OpenMode.ForRead) as BlockReference;
                            Debug.Print(blkRef.Name.ToUpper());
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage(ex.Message);
                }
                targetDoc.CloseAndDiscard();

 

Regards,

0 Likes
Reply
Accepted solutions (1)
1,248 Views
2 Replies
Replies (2)

norman.yuan
Mentor
Mentor
Accepted solution

You need to set CommandFlags.Session in the CommandMethod attribute, where the code is started, so that the newly opened document would become MdiActiveDocument. Then you need to lock the document when starting a Transition to access DB residing entities.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

Anonymous
Not applicable

Thank you, the Session flag worked perfectly. I did add a "using" statement to lock the document,  enclosing the transaction within it.

 

 

0 Likes