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
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?
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) };
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:
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()); } }
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") };
@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.
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;
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(); } }
Thanks for the response and the sample code.
I've tried it and I get an error on the foundBlocks.status? Not sure what the issue is.
Just tried it on the previous supplied Test.dwg without any problem.
These are the results:
Command: SME
INSERT HA-5, *Model_Space
INSERT HA-2, *Model_Space
Command: <Switching to: Layout1>
Regenerating layout.
Regenerating model - caching viewports.
Command: SPE
INSERT HA-2, *Paper_Space
[CommandMethod("SPE")] public static void SelectPaperspaceEntities() { 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, 1) //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(); } } [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(); } }
Well progress, I can get it to work as a command by itself, but as soon as I make it a function and run it within the rest of the code, it finds the block in paperspace?
Can't find what you're looking for? Ask the community or share your knowledge.