Apply layer modification to be able to select them

Apply layer modification to be able to select them

Anonymous
Not applicable
648 Views
5 Replies
Message 1 of 6

Apply layer modification to be able to select them

Anonymous
Not applicable

Hi all,

 

This is probably one of the most ridiculous questions I will ever ask, but I have to do it...

Once I have opened a transaction I am trying to unfreeze a specific layer and then in the same transaction be able to select it. Is this possible or am i required to open one transaction, apply the change and then open another to select the layer? My code (in part) is as follows.

Using tr As Transaction = doc.TransactionManager.StartTransaction()
                Dim bt As BlockTable = CType(tr.GetObject(db.BlockTableId, OpenMode.ForWrite), BlockTable)
                Dim btr As BlockTableRecord = CType(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
                Dim lt As LayerTable = tr.GetObject(db.LayerTableId, OpenMode.ForRead)

                ' Turn on the cabling layer just in case
                If lt.Has("Cabling") Then
                    Dim ltr As New LayerTableRecord()
                    ltr.Name = "Cabling"
                    lt.UpgradeOpen()
                    ltr.IsFrozen = False
                Else
                    ed.WriteMessage(vbLf & "No cabling exists...")
                    Return
                End If

                Dim psr As PromptSelectionResult = ed.GetSelection(New SelectionFilter(tv1))
                If (psr.Status <> PromptStatus.OK) Then
                    Return
                End If

I am not able to find a way to commit the layer change and then be able to select it. the layer change does not take effect and hence I am left with the program jumping out on the return. 

 

Does anyone have some advice that may help me to understand what i am doing wrong?

 

Thank you.

0 Likes
Accepted solutions (2)
649 Views
5 Replies
Replies (5)
Message 2 of 6

kerry_w_brown
Advisor
Advisor

It seems to me you are not commiting changes to the layer table record.

You seem to be unnecessarily making a new layer, which baffles me.

 

Have a look here for the basics

 

http://docs.autodesk.com/ACD/2011/ESP/filesMDG/WS1a9193826455f5ff2566ffd511ff6f8c7ca-3d1c.htm

 

 

added:

In fact, I can't see a LayerTable record in your code.

For your future sanity use a name something like mSpace in place of btr. 

I don't do VB so I won't say more.

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 3 of 6

kerry_w_brown
Advisor
Advisor

This is a more recent reference

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-NET/files/GUI...

 

but I'm wondering:

If the layer does not exist why are you creating it and making a selection set using an empty layer ??

 

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 4 of 6

kerry_w_brown
Advisor
Advisor
Accepted solution

 


@Anonymous ,

This seems to work OK for me.

http://www.theswamp.org/index.php?topic=55320.0

 

It's C# but you should be able to decipher it 🙂

 

The code may be revised when it is reviewed.

 

Hope it helps.

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 5 of 6

_gile
Consultant
Consultant
Accepted solution

Hi,

 

To be able to commit the 'thaw layer' stuff, you have to use a separated transaction.

It could be a nested one, but I think it's not a good practice to start a transaction if it may not be used.

In this case, I'd only start a the main transaction if the PromptSemectionResult.Status is OK.

 

Here's an example of how this can be done:

            // thaw the layer if exists
            // as none user interaction can occur during theis process, we use an OpenCloseTransaction which is cheaper
            using (var tr = new OpenCloseTransaction())
            {
                var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
                if (layerTable.Has("Cabling"))
                {
                    var layer = (LayerTableRecord)tr.GetObject(layerTable["Cabling"], OpenMode.ForRead);
                    if (layer.IsFrozen)
                    {
                        layer.UpgradeOpen();
                        layer.IsFrozen = false;
                    }
                }
                tr.Commit();
            }

            // build a selection filter and prompt the user for the selection
            var filter = new SelectionFilter(new[] { new TypedValue(8, "Cabling") });
            var selection = ed.GetSelection(filter);
            if (selection.Status != PromptStatus.OK) return;

            // we only start the main transaction if some entities have been selected
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var modelSpace = (BlockTableRecord)tr.GetObject(
                    SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);

                // do your stuff

                tr.Commit();
            }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 6

Anonymous
Not applicable

Thank you both for responding.

 

I had suspected this was the case, but wanted to make sure from people far more skilled than I.

 

BH

0 Likes