Which would be preferred and why?

Which would be preferred and why?

StormyC
Advocate Advocate
858 Views
4 Replies
Message 1 of 5

Which would be preferred and why?

StormyC
Advocate
Advocate

I've coded the following to obtain a list of layouts within open drawings.  The purpose is to obtain a full list of layout object ids so I can use them later.  I was wondering which would be best to use considering there will be 50ish drawings each with 5 layouts. By 'best' I mean 'most efficient' and 'best practice'. 

 

Alternatively is there an even better way to obtain a list of layout object ids???

 

 

Dim qDLs As IEnumerable(Of ObjectId) = _
From ll In acBlkTbl _
  Let f = CType(ll.GetObject(OpenMode.ForRead), BlockTableRecord) _
  Where f.IsLayout = True And f.Name <> "*MODEL_SPACE"
Select ObjID = f.LayoutId
Dim DwgLayoutIds1 = qDLs.ToList()

 And;

 

 

Dim DwgLayoutIds2 As New ObjectIdCollection
For Each BlkTblRecId As ObjectId In acBlkTbl    
Dim BlkTablRec As BlockTableRecord = acTrans.GetObject(BlkTblRecId, OpenMode.ForRead)
If BlkTablRec.IsLayout And BlkTablRec.Name <> "*MODEL_SPACE" Then                               DwgLayoutIds2.Add(BlkTablRec.LayoutId)
End If Next

 

(Apologies for the code formatting)

 

 

 

 

 

 

 

 

 

0 Likes
859 Views
4 Replies
Replies (4)
Message 2 of 5

StormyC
Advocate
Advocate

Here is yet another method of getting those layout object ids.

 

Dim acLayoutDict As DBDictionary = AcDb.LayoutDictionaryId.GetObject(OpenMode.ForRead, False)
Dim iLayoutDict As IDictionary = acLayoutDict
Dim LayoutIds As ICollection = iLayoutDict.Values
Dim DwgLayoutIds3 As New ObjectIdCollection
For Each obj As ObjectId In LayoutIds
  Dim LayObj As Layout = acTrans.GetObject(obj, OpenMode.ForRead, False)
  If LayObj.LayoutName <> "Model" Then
    DwgLayoutIds3.Add(obj)
  End If
Next

 

I pulled this together using a post from Tony Tanzillo regarding casting a DBDictionary to an IDictionary.

 

Any opinions anyone?  Or am I still missing the simplest way of getting the layout objectids, excluding model space...?

0 Likes
Message 3 of 5

_gile
Consultant
Consultant

Hi,

 

By my side, I'd directly step through the Layout Dictionary

 

 

            ObjectIdCollection layouts = new ObjectIdCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary dict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
                foreach (DBDictionaryEntry entry in dict)
                {
                    if (entry.Key != "Model")
                        layouts.Add(entry.Value);
                }
                tr.Commit();
            }

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 5

StormyC
Advocate
Advocate

Thanks Gile,

 

I had trouble getting the .values out of the dictionary - I was getting the 'Protected' error.

 

I will give your code a try.

0 Likes
Message 5 of 5

_gile
Consultant
Consultant

In fact, the way I prefer would be using F#:

 

 

let dict = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) :?> DBDictionary
let layouts = [for e in dict do if e.Key <> "Model" then yield e.Value]

 

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes