.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Reply
Message 1 of 8
quigs
672 Views, 7 Replies

for each?

Hi there,
My problem is as follows, my code below is for working out
the width of each object in a selection set. I have added a
line just to illustrate the measured section of each object.
I basically want to use these widths, else-where in my commeand, in
a collection. Or outside of the “for each”. For comparison
to each other
Is this possible or am I going about it in the wrong way?

Thanks,

Martin..

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Public Class lng
_
Public Sub lng()
'' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()
If acSSPrompt.Status = PromptStatus.OK Then
Dim acSSet As SelectionSet = acSSPrompt.Value
' For Each
For Each acSSObj As SelectedObject In acSSet
If Not IsDBNull(acSSObj) Then
Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite)
If Not IsDBNull(acEnt) Then
'' Draw Rectangles
Dim myExtents As Extents3d = (acEnt.GeometricExtents)
Dim P1 As Point3d = myExtents.MaxPoint
Dim P3 As Point3d = myExtents.MinPoint
Dim P2 As New Point3d(P3.X, P1.Y, 0)
Dim P4 As New Point3d(P1.X, P3.Y, 0)
' Draw Measure Line for Each object
Dim DimPCol As New Point3dCollection
DimPCol.Add(P1)
DimPCol.Add(P2)
Dim myPLine2 As New Polyline2d(Poly2dType.SimplePoly, DimPCol, 0, True, 0, 0, Nothing)
myPLine2.ColorIndex = 2 '1=Yellow 2=Red 3=Green 4=Cyan 5=Blue 6=Purple 7=White
' Get length of each line
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim tr As Transaction = db.TransactionManager.StartTransaction()
Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
btr.AppendEntity(myPLine2)
tr.AddNewlyCreatedDBObject(myPLine2, True)
tr.Commit()
End If
End If
Next
acTrans.Commit()
End If
End Using
End Sub
End Class Edited by: quigs on Dec 20, 2009 12:01 PM
My name is Martin.. 😄
7 REPLIES 7
Message 2 of 8
Mep09Developer
in reply to: quigs

I'm no expert on VB.NET selection sets as I haven't used any in my code yet. But, to take a stab at it, I wouldn't use SelectedObjects in the For Each. I'd probably try it like this:

For Each id As ObjectID In acSSet.GetObjectIds()
(indent) yada yada yada
Next

The reason being once the user has selected objects, there is nothing selected and you don't want your code trying to "select" the same objects as well.
Message 3 of 8
quigs
in reply to: quigs

Hi
Mep09Developer,
can you explain, why that would work, I'm still learning you see.

CheeRs,

Martin :D.
My name is Martin.. 😄
Message 4 of 8
Hallex
in reply to: quigs

You tried to create dtabase for every object in selection
and also extrafluous transaction

This one is working for me:

{code}
Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Geometry

Public Class lng

_
Public Sub lng()

'' Get the current document and database

Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

Dim acCurDb As Database = acDoc.Database

'Dim doc As Document = Application.DocumentManager.MdiActiveDocument

'Dim db As Database = doc.Database

Dim ed As Editor = acDoc.Editor

Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()

If acSSPrompt.Status = PromptStatus.OK Then

Dim acSSet As SelectionSet = acSSPrompt.Value

' For Each

For Each acSSObj As SelectedObject In acSSet

If Not IsDBNull(acSSObj) Then

Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite)

If Not IsDBNull(acEnt) Then

'' Draw Rectangles

Dim myExtents As Extents3d = (acEnt.GeometricExtents)

Dim P1 As Point3d = myExtents.MinPoint

Dim P3 As Point3d = myExtents.MaxPoint

Dim P2 As New Point3d(P3.X, P1.Y, 0)

Dim P4 As New Point3d(P1.X, P3.Y, 0)

' Draw Measure Line for Each object

Dim DimPCol As New Point3dCollection

DimPCol.Add(P1)

DimPCol.Add(P2)

DimPCol.Add(P3)

DimPCol.Add(P4)

Dim myPLine2 As New Polyline2d(Poly2dType.SimplePoly, DimPCol, 0, True, 0, 0, Nothing)

myPLine2.ColorIndex = 2 '1=Yellow 2=Red 3=Green 4=Cyan 5=Blue 6=Purple 7=White
myPLine2.Closed = True
' Get length of each line



Dim bt As BlockTable = DirectCast(acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead), BlockTable)

Dim btr As BlockTableRecord = DirectCast(acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)

btr.AppendEntity(myPLine2)

acTrans.AddNewlyCreatedDBObject(myPLine2, True)



End If

End If

Next

acTrans.Commit()

End If

End Using

End Sub

End Class
{code}

~'J'~
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 5 of 8
quigs
in reply to: quigs

So If I wanted to get the width of each box, say else where
in the code out of the for each, how would I do this?
My name is Martin.. 😄
Message 6 of 8
Hallex
in reply to: quigs

You are already there
Take a look at this line
{code}
Dim myExtents As Extents3d = (acEnt.GeometricExtents)
{code}

Just grab the difference between ordinates
and collect then where you want say in ArrayList etc

~'J'~
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 7 of 8
quigs
in reply to: quigs

DOOOOOHHH,
I wasn't declaring the collection outside (before) the
for each had started, it now works, thank you soooo
so so much for every ones patiance.

Much much kind christmas regards.

Quigs. :D..
My name is Martin.. 😄
Message 8 of 8
Hallex
in reply to: quigs

You're welcome

I would like to wish you a very nice Merry Christmas and a Great New Year

~'J'~
_____________________________________
C6309D9E0751D165D0934D0621DFF27919

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