AutoCAD Map 3D Developer
Welcome to Autodesk’s AutoCAD Map 3D Developer Forums. Share your knowledge, ask questions, and explore popular AutoCAD Map 3D Developer topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AutoCAD MAP attech and detach dwg files via a QueryModel

3 REPLIES 3
Reply
Message 1 of 4
JobvdSande
683 Views, 3 Replies

AutoCAD MAP attech and detach dwg files via a QueryModel

Hello,

 

Im trying to to clear the map from data. This is what is done:

click on the map -> data is added.

click on the map again -> data should be removed (if any was added).

 

I add the data using the following method:

Autodesk.Gis.Map.MapApplication mapApp = Autodesk.Gis.Map.HostMapApplicationServices.Application;
Autodesk.Gis.Map.Project.ProjectModel activeProject = mapApp.ActiveProject;
Autodesk.Gis.Map.Project.ProjectCollection progList = mapApp.Projects;
Autodesk.Gis.Map.Project.DrawingSet dwgSet = activeProject.DrawingSet;
            
Autodesk.Gis.Map.Project.AttachedDrawing dwgAttached = dwgSet.AttachDrawing(fileName);

Autodesk.Gis.Map.Query.QueryModel qryModel = activeProject.CurQuery;
qryModel.Mode = Autodesk.Gis.Map.Constants.QueryType.QueryDraw;
qryModel.Clear();
// The root query object
Autodesk.Gis.Map.Query.QueryBranch qb_root = Autodesk.Gis.Map.Query.QueryBranch.Create();


Autodesk.Gis.Map.Query.LocationCondition locationcondition = new Autodesk.Gis.Map.Query.LocationCondition();
locationcondition.LocationType = Autodesk.Gis.Map.Constants.LocationType.LocationCrossing;
locationcondition.Boundary = Autodesk.Gis.Map.Query.AllBoundary.Create();

// Make a branch Autodesk.Gis.Map.Query.QueryBranch qblocation = Autodesk.Gis.Map.Query.QueryBranch.Create(); qblocation.AppendOperand(locationcondition); // append it to the root qb_root.AppendOperand(qblocation); qryModel.Define(qb_root); // Execute it queried_keymap_objs_collection = qryModel.Execute(dwgSet);
dwgSet.DetachDrawing(fileName);

 

 This works, the data is added. However, now i click on the same area again. Now the added data has to be removed again..

 

But im stuck there.. I cant figure out how to remove the data.. Can anyone help me there?

 

Thank you!

 

3 REPLIES 3
Message 2 of 4
djonio
in reply to: JobvdSande

Sorry, I do not understand your question. Would you elaborate?

"click on the map -> data is added.

click on the map again -> data should be removed (if any was added)."

Why is any of this supposed to happen?

All I see is a: qryModel.Mode = Autodesk.Gis.Map.Constants.QueryType.QueryDraw;

Which added ths objects. Try "Preview Mode". A regen will remove the objects OR hang onto the objectIds of the collection and remove them yourself.

Or...maybe I just don't understand.

r,

dennis

Message 3 of 4
JobvdSande
in reply to: djonio

sorry. I try to be more clear.

 

The functionality is that when you select a function and you click somewhere on the map, that a background is loaded. This was first done by attaching a dwg file using the XREF method. This worked fine. But this caused performance issues when creating or expending features using this background.

 

I’m trying to use now a different method, this works great when doing it manual.

 

1) attach drawing 2) create query 3) define query (use location / window) 4) run query.

 

When doing this in code, it also works. As show in my first post.

 

Now in the original version, if you would click on the same spot again the previous loaded background is removed again. This is in the previous version easy by calling a detach function from the XREF method.

So far ok??

 

If i use the code i have now, the adding works great. I can’t use the .Preview() because I’m unable to use this background dwg file then for creating new features (using a fill/flood method).

 

The main issue I now have thus is, that after I added the data using the method I have now (see first post) that I’m unable to remove the data from the map again...

 

I can store the collection of objectID's indeed. I will try if I can remove those from the map indeed. Perhaps that works..

Message 4 of 4
djonio
in reply to: JobvdSande

        private int EraseAllInCollection(ObjectIdCollection c, ref Database _DB)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            int items_gone = 0;
            Transaction tr = doc.TransactionManager.StartTransaction();
            using (tr)
            {
                BlockTable bt = (BlockTable)tr.GetObject(_DB.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                foreach (ObjectId entId in c)
                {
                    DBObject obj = tr.GetObject(entId, OpenMode.ForWrite);
                    Entity ent = obj as Entity;
                    ent.Erase();
                    items_gone = items_gone + 1;
                }

                tr.Commit();
            }
            if (c.Count == items_gone)
                doc.Database.ReclaimMemoryFromErasedObjects(c);

            return items_gone;
        }

 Yes, this is very simple and it is very fast. The only drawback is that the DWG gets rather large when working with a _large_ number if objects.

I am aware that this is unsolicited but the following may help you get rid of those annoying query messages:

        static public int ErrSetLevel(short _lvl)
        {
            int resbuf_result_status = 0;
            ResultBuffer args1 = new ResultBuffer();
            args1.Add(new TypedValue((int)LispDataType.Text, "ade_errsetlevel"));
            args1.Add(new TypedValue((int)LispDataType.Int16, (short)_lvl));
            ResultBuffer resbuf_result1 = InvokeLisp(args1, ref resbuf_result_status);
            return resbuf_result_status;
        }
        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl)]
        extern static private int acedInvoke(IntPtr args, out IntPtr result);
        public static ResultBuffer InvokeLisp(ResultBuffer args, ref int stat)
        {
            IntPtr rb = IntPtr.Zero;
            stat = acedInvoke(args.UnmanagedObject, out rb);
            if (stat == (int)PromptStatus.OK && rb != IntPtr.Zero)
                return (ResultBuffer)DisposableWrapper.Create(typeof(ResultBuffer), rb, true);
            return null;
        }

 r,

dennis

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost