.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Find Layout name for object

9 REPLIES 9
Reply
Message 1 of 10
Martin60
2973 Views, 9 Replies

Find Layout name for object

I am currently changing from VBA to VB.Net.
I have code that finds all the inserts of a block but need to determine the layout in which that insert has been inserted.

In VBA I used:
sObjLayout = ThisDrawing.ObjectIdToObject(ssDwgSht.Item(i).OwnerID).Layout.Name

How do I do this in VB.Net

Thanks
Martin
9 REPLIES 9
Message 2 of 10
_gile
in reply to: Martin60

Hi

All entities (derived from DBObject class) have a OwnerId property.
Using GetObject in a transaction, you can get the Layout object, then it's name.


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 10
Anonymous
in reply to: Martin60

You say you're switching from VBA to VB.NET. Are you also switching from
the ActiveX API used by VBA, to the managed .NET API?

If so, you're probably going to need to review a lot of basics before you
can convert an existing VBA project to managed ObjectARX, but in any case,
you use the BlockId property an entity to get the ObjectId of the
BlockTableRecord that contains the entity.

Then, use that ObjectId to open the BlockTableRecord, and look at it's
LayoutId property, and then use that to open the Layout object, to get its
name.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6289340@discussion.autodesk.com...
I am currently changing from VBA to VB.Net.
I have code that finds all the inserts of a block but need to determine the
layout in which that insert has been inserted.

In VBA I used:
sObjLayout =
ThisDrawing.ObjectIdToObject(ssDwgSht.Item(i).OwnerID).Layout.Name


How do I do this in VB.Net

Thanks
Martin
Message 4 of 10
Martin60
in reply to: Martin60

Thanks for the information. I can see what you are saying but I am having a problem applying it.
Below is my code where tst is my selection set.
In this case they are all inserts.
I have highlighted the probelm line. (I have tried variuos methods all resulting in Fatal Errors).
Dim tst As EditorInput.PromptSelectionResult
tst = DwgSheetSelection("*")

Dim myDWG As ApplicationServices.Document
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction

Dim myObjIDs As DatabaseServices.ObjectIdCollection
Dim myObjID As DatabaseServices.ObjectId
Dim myBlkRef As DatabaseServices.BlockReference

myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myTransMan = myDWG.TransactionManager
myTrans = myTransMan.StartTransaction

If Not IsNothing(tst.Value) Then
myObjIDs = New DatabaseServices.ObjectIdCollection(tst.Value.GetObjectIds)
For Each myObjID In myObjIDs
myBlkRef = myObjID.GetObject(DatabaseServices.OpenMode.ForRead, False)
Dim myBTR As DatabaseServices.BlockTableRecord
myBTR = myBlkRef.BlockTableRecord.GetObject(DatabaseServices.OpenMode.ForRead)
Dim l As Layout = myBTR.LayoutId.GetObject(OpenMode.ForRead)

Next
End If

myTrans.Dispose()
myTransMan.Dispose()
Message 5 of 10
Anonymous
in reply to: Martin60

Sorry about that, but your code is hard to read in a newsreader with all the
wiki markup

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6289932@discussion.autodesk.com...
Thanks for the information. I can see what you are saying but I am having a
problem applying it.
Below is my code where tst is my selection set.
In this case they are all inserts.
I have highlighted the probelm line. (I have tried variuos methods all
resulting in Fatal Errors).
Dim tst As EditorInput.PromptSelectionResult
tst = DwgSheetSelection("*")

Dim myDWG As ApplicationServices.Document
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction

Dim myObjIDs As DatabaseServices.ObjectIdCollection
Dim myObjID As DatabaseServices.ObjectId
Dim myBlkRef As DatabaseServices.BlockReference

myDWG =
ApplicationServices.Application.DocumentManager.MdiActiveDocument

myTransMan = myDWG.TransactionManager
myTrans = myTransMan.StartTransaction

If Not IsNothing(tst.Value) Then
myObjIDs = New
DatabaseServices.ObjectIdCollection(tst.Value.GetObjectIds)
For Each myObjID In myObjIDs
myBlkRef =
myObjID.GetObject(DatabaseServices.OpenMode.ForRead, False)
Dim myBTR As DatabaseServices.BlockTableRecord
myBTR =
myBlkRef.BlockTableRecord.GetObject(DatabaseServices.OpenMode.ForRead)
Dim l As Layout =
myBTR.LayoutId.GetObject(OpenMode.ForRead)


Next
End If

myTrans.Dispose()
myTransMan.Dispose()
Message 6 of 10
Martin60
in reply to: Martin60

Re post without formating (hopes this works)
{code}
Dim tst As EditorInput.PromptSelectionResult
tst = DwgSheetSelection("*")

Dim myDWG As ApplicationServices.Document
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction

Dim myObjIDs As DatabaseServices.ObjectIdCollection
Dim myObjID As DatabaseServices.ObjectId
Dim myBlkRef As DatabaseServices.BlockReference

myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myTransMan = myDWG.TransactionManager
myTrans = myTransMan.StartTransaction

If Not IsNothing(tst.Value) Then
myObjIDs = New DatabaseServices.ObjectIdCollection(tst.Value.GetObjectIds)
For Each myObjID In myObjIDs
myBlkRef = myObjID.GetObject(DatabaseServices.OpenMode.ForRead, False)
Dim myBTR As DatabaseServices.BlockTableRecord
myBTR = myBlkRef.BlockTableRecord.GetObject(DatabaseServices.OpenMode.ForRead)

'Error next line
Dim l As Layout = myBTR.LayoutId.GetObject(DatabaseServices.OpenMode.ForRead)

Next
End If

myTrans.Dispose()
myTransMan.Dispose()
{/code}
Message 7 of 10
Anonymous
in reply to: Martin60

Let's start with this:

{code}

If Not IsNothing(tst.Value) Then
myObjIDs = New DatabaseServices.ObjectIdCollection(tst.Value.GetObjectIds)

{code}

No need to create an ObjectIdCollection because you can iterate over the
array of ObjectIds returned by GetObjectIds():

Here is a complete example:

{code}

// Translated by Reflector (http://www.redgate.net/reflector) (untested)

Public Class EntityHelper

' Takes an Entity and returns the name of the layout it is on, or
' an empty string if the entity is not in a layout block:

Public Shared Function GetLayoutName(ByVal entity As Entity) As String
Dim blockId As ObjectId = entity.BlockId
Dim btr As BlockTableRecord =
TryCast(blockId.GetObject(OpenMode.ForRead),BlockTableRecord)
If Not btr.LayoutId.IsNull Then
Dim layout As Layout =
TryCast(btr.LayoutId.GetObject(OpenMode.ForRead),Layout)
If (Not layout Is Nothing) Then
Return layout.LayoutName
End If
End If
Return String.Empty
End Function

' Usage example: Displays the name of the layout
' of each entity in a selection set:

Public Shared Sub Example()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim pso As New PromptSelectionOptions
Dim psr As PromptSelectionResult = doc.Editor.GetSelection
If (psr.Status = PromptStatus.OK) Then
Using tr As Transaction = doc.TransactionManager.StartTransaction
Dim i As Integer = 0
Dim id As ObjectId
For Each id In psr.Value.GetObjectIds
Dim entity As Entity =
TryCast(id.GetObject(OpenMode.ForRead),Entity)
Dim layoutName As String = EntityHelper.GetLayoutName(entity)
doc.Editor.WriteMessage(vbNewLine & "Item {0} Layout = {1}",
i, layoutName )
i += 1
Next
tr.Commit
End Using
End If
End Sub

End Class


{code}

In the code that follows that you are assuming that all objects in the
selection set are block references. Are they? If they're not you will get
an error when assigning the result of GetObject() to the BlockReference
variable.

In any case, you shouldn't be casting to a BlockReference to start with,
because the BlockId property you need to use to find the layout is a
property of Entity, so any entity will have it.

{code}

/// Translated from C# by Reflector

Public Class EntityHelper
' Methods
Public Shared Sub Example()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim pso As New PromptSelectionOptions
Dim psr As PromptSelectionResult = doc.Editor.GetSelection
If (psr.Status = PromptStatus.OK) Then
Using tr As Transaction = doc.TransactionManager.StartTransaction
Dim i As Integer = 0
Dim id As ObjectId
For Each id In psr.Value.GetObjectIds
Dim entity As Entity =
TryCast(id.GetObject(OpenMode.ForRead),Entity)
Dim layoutName As String = EntityHelper.GetLayoutName(entity)
doc.Editor.WriteMessage("Item {0} Layout = {1}", New Object()
{ i, layoutName })
i += 1
Next
tr.Commit
End Using
End If
End Sub

Public Shared Function GetLayoutName(ByVal entity As Entity) As String
Dim blockId As ObjectId = entity.BlockId
Dim btr As BlockTableRecord =
TryCast(entity.BlockId.GetObject(OpenMode.ForRead),BlockTableRecord)
If Not btr.LayoutId.IsNull Then
Dim layout As Layout =
TryCast(btr.LayoutId.GetObject(OpenMode.ForRead),Layout)
Return layout.LayoutName
End If
Return String.Empty
End Function

End Class

{code}



--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6289993@discussion.autodesk.com...
Re post without formating (hopes this works)
{code}
Dim tst As EditorInput.PromptSelectionResult
tst = DwgSheetSelection("*")

Dim myDWG As ApplicationServices.Document
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction

Dim myObjIDs As DatabaseServices.ObjectIdCollection
Dim myObjID As DatabaseServices.ObjectId
Dim myBlkRef As DatabaseServices.BlockReference

myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myTransMan = myDWG.TransactionManager
myTrans = myTransMan.StartTransaction

If Not IsNothing(tst.Value) Then
myObjIDs = New DatabaseServices.ObjectIdCollection(tst.Value.GetObjectIds)
For Each myObjID In myObjIDs
myBlkRef = myObjID.GetObject(DatabaseServices.OpenMode.ForRead, False)
Dim myBTR As DatabaseServices.BlockTableRecord
myBTR =
myBlkRef.BlockTableRecord.GetObject(DatabaseServices.OpenMode.ForRead)

'Error next line
Dim l As Layout =
myBTR.LayoutId.GetObject(DatabaseServices.OpenMode.ForRead)

Next
End If

myTrans.Dispose()
myTransMan.Dispose()
{/code}
Message 8 of 10
Martin60
in reply to: Martin60

Thanks for your help.
Though I cannot say I fully understand I can get layout name and hopefully understanding will follow.

I am certain this particular code will only process inserts as I applied a filter to my function that retrieves the selection set.

My main problem at the moment is there appears to be different methods programmers use for the same results. In particular the way Transactions are managed. Though this perception may be due to my lack of knowledge.

The fog is getting less though.

Thanks again.
Message 9 of 10
Martin60
in reply to: Martin60

How do I post code formatted like yours?
Message 10 of 10
arcticad
in reply to: Martin60

Formatting Code Information.
http://discussion.autodesk.com/forums/ann.jspa?annID=126
---------------------------



(defun botsbuildbots() (botsbuildbots))

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost