Jump to content

Recommended Posts

Posted

can somebody post some sample code or guidlines on how to position an attribute in a block constructed in VB.Net?

 

I am making a block comprising a triangle (pline - -6,0:6,0:0,10 & close) and an attribute, centre justified. I want the base point of the trianle to be in the middle of its base (done) and the attribute to be 0,3 but I can't seem to be able to position anywhere other than 0,0.

 

(I know I can draw the triangle 3mm lower but that wouldn't put the insert point where I want.)

Posted

Try this code, change to your suit

 
       <CommandMethod("Tri", CommandFlags.Modal Or CommandFlags.Redraw)> _
       Public Shared Sub CreateTriangleMark()
           ' Get the current document and database
           Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
           Dim db As Database = doc.Database
           Dim ed As Editor = doc.Editor
           Using tr As Transaction = db.TransactionManager.StartTransaction()
               Try
                   Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForWrite), BlockTable)
                   ' get current text size for attribute or use hard coded one instead
                   Dim txtheight As Double = db.Textsize
                   ' block name
                   Dim blkName As String = "TRIANGLE"
                   ' check if exists
                   If bt.Has(blkName) Then
                       ed.WriteMessage(vbLf & "Block ""TRIANGLE"" already exist.")
                       ' if exists then exit
                       Return
                   End If
                   Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                   Dim inspt As New Point3d(0, 0, 0) '' perrhaps use: New Point3d(0, 5, 0) not sure about
                   Dim newBtr As New BlockTableRecord()
                   bt.Add(newBtr)
                   ' change settings for you block to your suit:
                   newBtr.Name = blkName
                   newBtr.Origin = inspt
                   newBtr.BlockScaling = BlockScaling.Uniform
                   newBtr.Units = UnitsValue.Inches
                   newBtr.Explodable = False
                   tr.AddNewlyCreatedDBObject(newBtr, True)
                   Dim pline As Polyline = New Polyline(3)
                   pline.AddVertexAt(0, New Point2d(-6, 0), 0, 0, 0)
                   pline.AddVertexAt(1, New Point2d(6, 0), 0, 0, 0)
                   pline.AddVertexAt(2, New Point2d(0, 10), 0, 0, 0)
                   pline.Closed = True
                   pline.Layer = "0"
                   pline.LinetypeId = db.ContinuousLinetype
                   '' color by block
                   pline.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0)
                   '' lweight by block
                   pline.LineWeight = LineWeight.ByBlock
                   newBtr.AppendEntity(pline)
                   tr.AddNewlyCreatedDBObject(pline, True)
                   Dim attr As New AttributeDefinition()
                   attr.Layer = "0"
                   attr.LinetypeId = db.ContinuousLinetype
                   attr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0)
                   ' color by block
                   attr.LineWeight = LineWeight.ByLineWeightDefault
                   attr.Tag = "DESCRIPTION"
                   attr.Prompt = "Description:"
                   attr.TextString = "blah"
                   attr.Preset = True
                   ' using current textstyle
                   'attr.TextStyle = db.Textstyle ''<--   A2009
                   attr.TextStyleId = db.Textstyle ''<--   A2010
                   attr.Height = txtheight
                   attr.Position = New Point3d(0, 5, 0)
                   '' apply alignment
                   attr.Justify = AttachmentPoint.MiddleCenter
                   attr.AlignmentPoint = attr.Position
                   attr.LockPositionInBlock = True
                   attr.AdjustAlignment(db)
                   newBtr.AppendEntity(attr)
                   tr.AddNewlyCreatedDBObject(attr, True)
                   tr.Commit()
                   ed.Regen()
               Catch ex As Autodesk.AutoCAD.Runtime.Exception
                   ed.WriteMessage(ex.Message + vbLf + ex.StackTrace)
               End Try
           End Using
       End Sub

Posted (edited)

Thanks Fixo, I'll give that a try (don't knowwhy I said try, you've never let me down) on Monday.

 

A couple of things to ask you

 

I am now on 2013. I notice you have commented the differences between 2009 & 2010. I assume I will see the correct format on the command line when I code it. Likewise the changes between metric & imperial will be obvious.

 

from memory I didn't do these statements

attr.AlignmentPoint = attr.Position 
attr.AdjustAlignment(db)

Is it that that adjusts the position of the attribute? Presumably these lines need repeating for each attribute being positioned.

 

And one question,

 

Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForWrite), BlockTable)

what does DirectCast do? I have not seen that command yet.

 

------------------------------------------------------

 

and another question that I know I asked (with your other user name) but have forgotten your answer. Where is Pietari, Venäjä?

 

my google search brings up St Petersburg, Russia - is that correct? If it is, I intend visiting in a few years time (2015!) and can I ask some "tourist" questions.

Edited by dbroada
"Chat" question added
Posted

additional to edit, I have found that you do live in St Petersburg.

 

I have a grand tour by train planned for a couple of years time so you may see a pm from me later asking about best time of year to visit Russia etc.

Posted

Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForWrite), BlockTable)

 

what does DirectCast do? I have not seen that command yet.

 

I just learned this the other week....

 

DirectCast-ing results in either a Typed Object (the Type you've just casted to), or an Exception.

 

Casting an object to a Type with the As operator results in either a valid, Typed Object, or Nothing (Null in C#).

 

Lastly, the Is operator can be used to qualify if an Object is the Typed Object being compared against, discards the resultant casted Object or Exception, and returns a bool (i.e., true / false).

Posted

fixo, thanks, that worked how I wanted.

 

BB, and thank you. Still not sure whatit all means just yet though. :D :rtfm:

Posted

this.Mode = Modes.Rant;

 

I just spent, what is I'm sure too long, writing a summary of what I described above, including code samples in VB and C#, only for CADTutor to inform me when I hit the 'Go Advanced' button that I needed to hit the back button... At which time everything was lost, and only this blank post remained. tickedoff.gif

 

Just wanted to let you know that I tried, and that I might give it another go, another time... once I cool off. *ticked off*

Posted

I thank you for your effort and know your frustrations. That has happened tp me several times in the past. A contributing factor towards my answers being of few words....

 

 

hopefully this will help you get to

this.Mode = Modes.Calm

 

:beer: :popcorn: :beer: :playing: :beer:

Posted
this.Mode = Modes.Rant;

 

I just spent, what is I'm sure too long, writing a summary of what I described above, including code samples in VB and C#, only for CADTutor to inform me when I hit the 'Go Advanced' button that I needed to hit the back button... At which time everything was lost, and only this blank post remained. tickedoff.gif

 

Just wanted to let you know that I tried, and that I might give it another go, another time... once I cool off. *ticked off*

 

That happened to me also a couple of times. Now when I go from 'Quick Reply' to 'Go Advanced' I always copy all of my new post to the clipboard, just in case it gets lost when switching over. Perhaps David could incorporate a function to save a draft of a post, just so it does not "get lost".

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...