.NET

Reply
Valued Contributor
cjacobi
Posts: 54
Registered: ‎11-12-2012
Message 1 of 13 (456 Views)

Nested block position in model/paper space.

456 Views, 12 Replies
05-31-2013 01:08 PM

Hi,  I need to gather objects on a specivied layername within model_space in C#.

 

How do I add a fileter to my filtervalues to only grab model_space objects.

 

 

TypedValue

[] filterValues = new[] { newTypedValue((int)DxfCode.LayerName, LayerName.ToUpper()) };

SelectionFilter filter = newSelectionFilter(filterValues);

PromptSelectionResult foundBlocks = this.ed.SelectAll(filter);

SelectionSet currSS = foundBlocks.Value;

 

or

 

how to I check after I get the object and check the blockname

 

currEnt = (Entity)objID.GetObject(OpenMode.ForWrite);

                           

if (currEnt.BlockName != "*Model_Space")

 

Please if someone could give me some guidence with the syntax.

 

thanks

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

Re : Nested block position in model/paper space.

05-31-2013 01:18 PM in reply to: cjacobi

Hi,

 

TypedValue[] filterValues = new[] 
{
newTypedValue(8, LayerName),
new TypedValue(410, "Model")
};

 

Gilles Chanteau
Valued Contributor
cjacobi
Posts: 54
Registered: ‎11-12-2012
Message 3 of 13 (450 Views)

Re : Nested block position in model/paper space.

05-31-2013 01:38 PM in reply to: _gile

thanks Gilles,

 

I've changed my code to this

 

TypedValue[] filterValues = new[] { new TypedValue(8, LayerName.ToUpper()), new TypedValue(410, "Model") };

 

but it still is picking up my object in paper space? not sure why it's not working. Any ideas? this seems simple enough, what am I doing wrong?

Valued Contributor
cjacobi
Posts: 54
Registered: ‎11-12-2012
Message 4 of 13 (447 Views)

Re : Nested block position in model/paper space.

05-31-2013 01:49 PM in reply to: cjacobi

have tried the following too with no success, based on the DXF code for model_space in the Autodesk documentation

 

TypedValue[] filterValues = new[] { newTypedValue(8, LayerName.ToUpper()), newTypedValue(67, 0) };

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 5 of 13 (431 Views)

Re : Nested block position in model/paper space.

05-31-2013 06:56 PM in reply to: _gile

Hi Gile.

 

Not sure what localized version of AutoCAD the OP is using, but the name of the Model tab is localized, and there's no API I know if in the SymbolUtilityServices class that provides that, so you have to open the Layout to get the localized name in order to use it with a selection filter.

 

An example:

 

http://forums.autodesk.com/t5/NET/Send-Command-Executes-after-exiting-command-method/m-p/3882929/hig...

 

Valued Contributor
Ertqwa
Posts: 88
Registered: ‎10-03-2011
Message 6 of 13 (417 Views)

Re: Nested block position in model/paper space.

05-31-2013 11:14 PM in reply to: cjacobi

Hi,

 

you could try 'SymbolUtilityServices.GetBlockModelSpaceId(database)'.

 

Example (AutoCAD 2012):

 

 [CommandMethod("TestLayerAndLayout")]
        public void TestLayerAndLayout()
        {
            Document objDocument;
            TypedValue[] objTypedValueList = null;
            SelectionFilter objSelectionFilter = null;
            PromptSelectionResult objPromptSelectionResult = null;
            SelectionSet objSelectionSet = null;
            Entity entItem;

            try
            {
                // Get document.
                objDocument = CADS.Application.DocumentManager.MdiActiveDocument;
                using (Transaction objTransaction = objDocument.Database.TransactionManager.StartTransaction())
                {
                    // Create a TypedValue array to define the filter criteria.
                    objTypedValueList = new TypedValue[1];
                    objTypedValueList.SetValue(new TypedValue((int)DxfCode.LayerName, "0"), 0);
                    objSelectionFilter = new SelectionFilter(objTypedValueList);
                    // Select all.
                    objPromptSelectionResult = objDocument.Editor.SelectAll(objSelectionFilter);
                    if (objPromptSelectionResult.Status == PromptStatus.OK)
                    {
                        // Get selection set.
                        objSelectionSet = objPromptSelectionResult.Value;
                        // Loop selection set.
                        foreach (SelectedObject objSelectedObject in objSelectionSet)
                        {
                            if (objSelectedObject != null)
                            {
                                // Get entity.
                                entItem = objTransaction.GetObject(objSelectedObject.ObjectId, OpenMode.ForRead) as Entity;
                                if (entItem != null)
                                {
                                    if (entItem.OwnerId == SymbolUtilityServices.GetBlockModelSpaceId(objDocument.Database))
                                    {
                                        objDocument.Editor.WriteMessage("\n Entity {0} is in modelspace.", entItem.GetType().ToString());
                                    }
                                    else
                                    {
                                        objDocument.Editor.WriteMessage("\n Entity {0} is NOT in modelspace.", entItem.GetType().ToString());
                                    }
                                }
                                else
                                {
                                    objDocument.Editor.WriteMessage("\n One of the selected items is NULL and could not be processed.");
                                }
                            }
                            else
                            {
                                objDocument.Editor.WriteMessage("\n One of the selected objects is NULL and could not be processed.");
                            }
                        }
                    }
                    objTransaction.Commit();
                }
            }
            catch (System.Exception Ex)
            {
                CADS.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Error testing if objects are in modelspace (layer 0 only): " + Ex.ToString());
            }
        }

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 7 of 13 (392 Views)

Re : Nested block position in model/paper space.

06-01-2013 02:23 PM in reply to: cjacobi

I think the reason if this fail on your filter is not indexed right

TypedValue[] filterValues = new TypedValue[2]
 { 
new TypedValue(8, LayerName.ToUpper()),
 new TypedValue(410, "Model")
 };

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 8 of 13 (374 Views)

Re : Nested block position in model/paper space.

06-03-2013 07:10 PM in reply to: Hallex

Hallex wrote:

I think the reason if this fail on your filter is not indexed right

TypedValue[] filterValues = new TypedValue[2]
 { 
new TypedValue(8, LayerName.ToUpper()),
 new TypedValue(410, "Model")
 };

 


You don't pass an index for the array size when you initialize it with values.

Valued Contributor
cjacobi
Posts: 54
Registered: ‎11-12-2012
Message 9 of 13 (252 Views)

Re : Nested block position in model/paper space.

01-03-2014 12:53 PM in reply to: DiningPhilosopher

Finally getting back to this problem. I only get time to program here and there.

 

So, after people's suggestions this is what I have and it still keeps picking up the darn block in paperspace. I added the If statement to try and catch it using the symbolutilityservice but they all fall into the true part of the If. I have attached a sample drawing of what I'm working with, just in case that helps. I can't believe this is so difficult to resolve....

 

There are currently three blocks in the drawing, 2 in modelspace, 1 in paperspace. My currSS.Count returns 3, and all 3 fall into the true statement of the CurrEnt.OwnerID == SymbolUtilityServices.GetBlockModelSpaceId(this.dwgDB)

 

Any help or suggestions is greatly appreciated!!!

 

 

Happy New Years to everyone!

 

cj

 

 using (Transaction trans = this.dwgDB.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = null;

                try
                {
                    //  Get all objects on the databox layer in model space.


                    TypedValue[] filterValues = new TypedValue[3]
                    {
                    new TypedValue(0, "INSERT"), new TypedValue(8, LayerName.ToUpper()),new TypedValue(410, "Model")
                    };                  

                    SelectionFilter filter = new SelectionFilter(filterValues);
                    PromptSelectionResult foundBlocks = this.ed.SelectAll(filter);
                    SelectionSet currSS = foundBlocks.Value;


                    if (currSS != null && currSS.Count > 0)
                    {
                        Entity currEnt;
                        BlockReference blockRef;
                        ObjectId[] objs = currSS.GetObjectIds();

                        foreach (ObjectId objID in objs)
                        {
                            currEnt = (Entity)objID.GetObject(OpenMode.ForWrite);

                            if (currEnt.OwnerId == SymbolUtilityServices.GetBlockModelSpaceId(this.dwgDB))
                            {
                                this.ed.WriteMessage("\n Entity {0} is in modelspace.", currEnt.GetType().ToString());
                            }
                            else
                            {
                               this.ed.WriteMessage("\n Entity {0} is NOT in modelspace.", currEnt.GetType().ToString());
                            }


                            if (currEnt is BlockReference)
                            {
                                blockRef = (BlockReference)currEnt;

 

Mentor
SENL1362
Posts: 150
Registered: ‎07-20-2011
Message 10 of 13 (243 Views)

Re : Nested block position in model/paper space.

01-03-2014 01:42 PM in reply to: cjacobi

Please try this:

 

        [CommandMethod("SME")]
        public static void SelectModelspaceEntities()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TypedValue[] filterValues = new TypedValue[]
                {                    
                    new TypedValue((int)DxfCode.Start, "INSERT"),
                    new TypedValue((int)DxfCode.LayerName, "0"),
                    new TypedValue((int)67, 0)    //Modelspace::67=0, Paperspace::67=1
                    };
                SelectionFilter filter = new SelectionFilter(filterValues);
                PromptSelectionResult foundBlocks = ed.SelectAll(filter);
                if (foundBlocks.Status!=PromptStatus.OK)
                    return;

                foreach(ObjectId id in foundBlocks.Value.GetObjectIds())
                {
                    ed.WriteMessage("\n{0}", id.ObjectClass.DxfName);
                    BlockReference br = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                    ed.WriteMessage("\t{0}, {1}", br.Name, br.BlockName);
                    
                }
                tr.Commit();
            }
        }

 

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