How to use TypedValue?

How to use TypedValue?

Anonymous
Not applicable
5,481 Views
7 Replies
Message 1 of 8

How to use TypedValue?

Anonymous
Not applicable

I'm trying to create a dynamic filter, sometimes the value can be "ALL".

 

But I'm not knowing how to make.

 

 

        public static ObjectId[] FilterDimension(string LayerName, string Start, string ColorName, string LinetypeName)
        {
            try
            {
                Document acDoc = Application.DocumentManager.MdiActiveDocument;
                Database acCurDb = acDoc.Database;
                Editor editor = acDoc.Editor;
                TypedValue[] typedValue = new TypedValue[4];
                typedValue.SetValue(new TypedValue((int)DxfCode.Start, (Start != "ALL") ? Start : "*"), 0);
                typedValue.SetValue(new TypedValue((int)DxfCode.LayerName, LayerName), 1);
                typedValue.SetValue(new TypedValue((int)DxfCode.ColorName, (ColorName != "ALL") ? ColorName : "*"), 2);
                typedValue.SetValue(new TypedValue((int)DxfCode.LinetypeName, (LinetypeName != "ALL") ? LinetypeName : "*"), 3);
                SelectionFilter selectionFilter = new SelectionFilter(typedValue);
                ObjectId[] objectIdList = editor.SelectAll(selectionFilter).Value.GetObjectIds();
                return objectIdList;
            }
            catch (System.Exception)
            {
                return null;
            }
        }

 

Help me, please.

Thank you!

0 Likes
5,482 Views
7 Replies
Replies (7)
Message 2 of 8

chiefbraincloud
Collaborator
Collaborator

Your approach works fine for me, what is the problem?

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 3 of 8

chiefbraincloud
Collaborator
Collaborator

This line:

 

ObjectId[] objectIdList = editor.SelectAll(selectionFilter).Value.GetObjectI​ds();

will cause an error if the PromptStatus of the selection is not OK, so you should create a PromptSelectionResult, and check the PromptStatus before trying to get access the Value property.

Dave O.                                                                  Sig-Logos32.png
Message 4 of 8

Anonymous
Not applicable

I did like this:

public static ObjectId[] FilterDimension(string LayerName, string Start, string ColorName, string LinetypeName)
        {
            try
            {
                Document acDoc = Application.DocumentManager.MdiActiveDocument;
                Database acCurDb = acDoc.Database;
                Editor editor = acDoc.Editor;
                List<TypedValue> typedValuelist = new List<TypedValue>();
                
                typedValuelist.Add(new TypedValue((int)DxfCode.Operator, "<and"));
                typedValuelist.Add(new TypedValue((int)DxfCode.LayerName, LayerName));
                if (Start != "ALL")
                    typedValuelist.Add(new TypedValue((int)DxfCode.Start, Start));
               /*if (ColorName != "ALL")
                    typedValuelist.Add(new TypedValue((int)DxfCode.ColorName, Color.FromColorIndex(ColorMethod.ByAci, GetIndexLayer(ColorName))));*/
                if (LinetypeName != "ALL")
                    typedValuelist.Add(new TypedValue((int)DxfCode.LinetypeName, LinetypeName));
                typedValuelist.Add(new TypedValue((int)DxfCode.Operator, "and>"));
                SelectionFilter selectionFilter = new SelectionFilter(typedValuelist.ToArray());
                return editor.SelectAll(selectionFilter).Value.GetObjectIds();
            }
            catch (System.Exception)
            {
                return null;
            }
        }


public static short GetIndexLayer(string layer)
        {
            try
            {
                return (short)Convert.ToInt32(layer);
            }
            catch (System.Exception)
            {
                if (layer.ToUpper() == "RED")
                    return (short)1;
                if (layer.ToUpper() == "YELLOW")
                    return (short)2;
                if (layer.ToUpper() == "GREEN")
                    return (short)3;
                if (layer.ToUpper() == "CYAN")
                    return (short)4;
                if (layer.ToUpper() == "BLUE")
                    return (short)5;
                if (layer.ToUpper() == "MAGENTA")
                    return (short)6;
                if (layer.ToUpper() == "WHITE")
                    return (short)7;
            }
            return (short)1;
        }

 

 

The problem is with the line:

 

if (ColorName != "ALL")
                    typedValuelist.Add(new TypedValue((int)DxfCode.ColorName, Color.FromColorIndex(ColorMethod.ByAci, GetIndexLayer(ColorName))));

 

If I add it goes wrong.

0 Likes
Message 5 of 8

chiefbraincloud
Collaborator
Collaborator

I see now that is because you are passing a Color object to the typed value for ColorName which should just be a string.  (The ColorName (= 430) dxfcode is also not documented in my old reference, so I'm not sure it would work anyway)

 

Use DxfCode.Color (= 62) and pass the color number.

 

 

if (ColorName != "ALL")
                    typedValuelist.Add(new TypedValue((int)DxfCode.Color, GetIndexLayer(ColorName)));

 

Or better still, just pass the Color number to the FilterDimension Function instead of passing the name, and converting that to the number.

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 6 of 8

Anonymous
Not applicable

Ok! I'll try, I had tested, but I used string, must be why it went wrong.

0 Likes
Message 7 of 8

BKSpurgeon
Collaborator
Collaborator

HI

i'm having a similar issue to you, understanding how to use the typed value struct. (The ObjectArx discussion on managed wrappers and result buffers doesn't make much sense to me.) here is the issue/

 


typedValue.SetValue(new TypedValue((int)DxfCode.LayerName, LayerName), 1);

 

 

in the above case, we have the layer name. What does a typecode value of 1 mean? Why not 2, or three? I do not understand the significance of the type code number here.

 

 

 

 

0 Likes
Message 8 of 8

Anonymous
Not applicable

>typedValue.SetValue(new TypedValue((int)DxfCode.LayerName, LayerName),1);

 

You do not have to take that route, do a more search about these type of calls usage or implementations, there must be tons of samples so far.

 

You can try something like this, with the use of an object initializer:

 

TypedValue[] values = { new TypedValue((int)DxfCode.Start, "SOLID") }; // then just add any other value after placing a ',' comma separator:

 

TypedValue[] values = { new TypedValue((int)DxfCode.Start, "SOLID"), new TypeValue..... };

 

HTH.-