.NET

Reply
Mentor
truss_85
Posts: 173
Registered: ‎02-13-2011
Message 11 of 16 (173 Views)

Re: Can not snap locked layers

05-14-2012 02:10 AM in reply to: Hallex

No Problem

I think I did not express my self clearly.

Thanks again

Distinguished Mentor
BlackBox_
Posts: 733
Registered: ‎02-25-2013
Message 12 of 16 (76 Views)

Re : Can not snap locked layers

03-25-2014 07:47 AM in reply to: _gile

_gile wrote:

 

... I didn't find no way to get it work with the INTersect osnap.

 


I've just stumbled across this thread, and your post looks to be very useful, Gile - Thank you.

 

As I am just starting to researching this topic, and you may already be aware, Kean shows how to deal with INTersect osnap using GeometryOverrule here:

 

http://through-the-interface.typepad.com/through_the_interface/2013/12/disabling-snapping-to-specifi...

 

 

 

Cheers



"Potential has a shelf life." - Margaret Atwood


Autodesk Exchange Apps ~ Autoloader ~ AutoCAD Security

*Expert Elite*
_gile
Posts: 2,072
Registered: ‎04-29-2006
Message 13 of 16 (72 Views)

Re : Can not snap locked layers

03-25-2014 08:24 AM in reply to: BlackBox_

Thanks BlackBox_.

 

I saw Kean's thread sometimes ago. I rewrote the code to publish it on a French web site: CADxp.

 

Two toggle commands are defined. Both can be called transparently.

LOKO to (de)activate osnaps on locked layers.

XROS to (de)activate osnaps on xrefs.

 

Here's the code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(Gile.LockOsnap.CommandMethods))]

namespace Gile.LockOsnap
{
    public class CommandMethods
    {
        private OSOverrule lokoOSOverrule, xrosOSOverrule;
        private IntOverrule lokoIntOverrule, xrosIntOverrule;

        [CommandMethod("LOKO", CommandFlags.Modal | CommandFlags.Transparent)]
        public void LockOsnapCmd()
        {
            ToggleOsnaps(ref lokoOSOverrule, ref lokoIntOverrule, lokoPromptingForPoint, LockedUpdate, "calques verrouillés");
        }

        [CommandMethod("XROS", CommandFlags.Modal | CommandFlags.Transparent)]
        public void XrefOsnapCmd()
        {
            ToggleOsnaps(ref xrosOSOverrule, ref xrosIntOverrule, xrosPromptingForPoint, XrefUpdate, "références externes");
        }

        void lokoPromptingForPoint(object sender, PromptPointOptionsEventArgs e)
        {
            LockedUpdate();
        }

        void xrosPromptingForPoint(object sender, PromptPointOptionsEventArgs e)
        {
            XrefUpdate();
        }

        private void ToggleOsnaps(
            ref OSOverrule osOverrule,
            ref IntOverrule intOverrule,
            PromptPointOptionsEventHandler handler, 
            Action update, 
            string msg)
        {
            Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            RXClass entityClass = RXClass.GetClass(typeof(Entity));
            if (osOverrule == null)
            {
                ed.PromptingForPoint += handler;
                osOverrule = new OSOverrule();
                Overrule.AddOverrule(entityClass, osOverrule, false);
                intOverrule = new IntOverrule();
                Overrule.AddOverrule(entityClass, intOverrule, false);
                ed.WriteMessage(string.Format("\nAccrochages sur les {0} désactivés.", msg));
                update();
                Overrule.Overruling = true;
            }
            else
            {
                ed.PromptingForPoint -= xrosPromptingForPoint;
                Overrule.RemoveOverrule(entityClass, osOverrule);
                osOverrule = null;
                Overrule.RemoveOverrule(entityClass, intOverrule);
                intOverrule = null;
                ed.WriteMessage(string.Format("\nAccrochages sur les {0} activés.", msg));
            }
        }

        private void LockedUpdate()
        {
            if (lokoOSOverrule != null)
            {
                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
                {
                    HashSet<string> layers = new HashSet<string>(
                        ((LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead))
                        .Cast<ObjectId>()
                        .Select(id => (LayerTableRecord)tr.GetObject(id, OpenMode.ForRead))
                        .Where(ltr => ltr.IsLocked)
                        .Select(ltr => ltr.Name));
                    ObjectId[] onLocked = ((BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead))
                        .Cast<ObjectId>()
                        .Where(id => layers.Contains(((Entity)tr.GetObject(id, OpenMode.ForRead)).Layer))
                        .ToArray();
                    lokoOSOverrule.SetIdFilter(onLocked);
                    lokoIntOverrule.SetIdFilter(onLocked);
                    tr.Commit();
                }
            }
        }

        private void XrefUpdate()
        {
            if (xrosOSOverrule != null)
            {
                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
                {
                    ObjectId[] xrIds = ((BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead))
                        .Cast<ObjectId>()
                        .Select(id => (BlockTableRecord)tr.GetObject(id, OpenMode.ForRead))
                        .Where(btr => btr.IsFromExternalReference)
                        .SelectMany(btr => btr.GetBlockReferenceIds(true, false).Cast<ObjectId>())
                        .ToArray();
                    xrosOSOverrule.SetIdFilter(xrIds);
                    xrosIntOverrule.SetIdFilter(xrIds);
                    tr.Commit();
                }
            }
        }

        class OSOverrule : OsnapOverrule
        {
            public override void GetObjectSnapPoints(
              Entity ent,
              ObjectSnapModes mode,
              IntPtr gsm,
              Point3d pick,
              Point3d last,
              Matrix3d view,
              Point3dCollection snap,
              IntegerCollection geomIds) { }

            public override void GetObjectSnapPoints(
              Entity ent,
              ObjectSnapModes mode,
              IntPtr gsm,
              Point3d pick,
              Point3d last,
              Matrix3d view,
              Point3dCollection snaps,
              IntegerCollection geomIds,
              Matrix3d insertion) { }

            public override bool IsContentSnappable(Entity entity)
            {
                return false;
            }
        }

        class IntOverrule : GeometryOverrule
        {
            public override void IntersectWith(
              Entity ent1,
              Entity ent2,
              Intersect intType,
              Plane proj,
              Point3dCollection points,
              IntPtr thisGsm,
              IntPtr otherGsm) { }

            public override void IntersectWith(
              Entity ent1,
              Entity ent2,
              Intersect intType,
              Point3dCollection points,
              IntPtr thisGsm,
              IntPtr otherGsm) { }
        }
    }
}

 

 

Gilles Chanteau
Distinguished Mentor
BlackBox_
Posts: 733
Registered: ‎02-25-2013
Message 14 of 16 (66 Views)

Re : Can not snap locked layers

03-25-2014 09:35 AM in reply to: _gile

_gile wrote:

Thanks BlackBox_.

 

I saw Kean's thread sometimes ago. I rewrote the code to publish it on a French web site: CADxp.

 


I suspected you would have, as I know you to be well informed, but had to take the chance at being of some small amount of help to one who has been so helpful to me in the past.

 

 

 


_gile wrote:

 

Two toggle commands are defined. Both can be called transparently.

LOKO to (de)activate osnaps on locked layers.

XROS to (de)activate osnaps on xrefs. 

 



While I have not yet tested for myself, your description, and a quick glance at the code leaves me with only one thought... Simply brilliant.

 

 

 

Separately, I often come across your posts when trying to accomplish a task, or learn how to go about something. Do you by chance have a website, etc. that I might be able to read tips & tutorials on the myriad facets of the AutoCAD .NET API? 

 

Cheers



"Potential has a shelf life." - Margaret Atwood


Autodesk Exchange Apps ~ Autoloader ~ AutoCAD Security

*Expert Elite*
_gile
Posts: 2,072
Registered: ‎04-29-2006
Message 15 of 16 (62 Views)

Re : Can not snap locked layers

03-25-2014 09:57 AM in reply to: BlackBox_

I have a (very) little web site (gilecAD) but there're not so much tutos and all in French.

Gilles Chanteau
Distinguished Mentor
BlackBox_
Posts: 733
Registered: ‎02-25-2013
Message 16 of 16 (58 Views)

Re : Can not snap locked layers

03-25-2014 10:17 AM in reply to: _gile

_gile wrote:

I have a (very) little web site (gilecAD) but there're not so much tutos and all in French.


You have more content on your site than I do; thanks for sharing. :thumbsup:



"Potential has a shelf life." - Margaret Atwood


Autodesk Exchange Apps ~ Autoloader ~ AutoCAD Security

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community