Using VB.net I would like to insert General Note objects into the drawing.
Could someone please provide a snippet of creating an instance of said object?
TIA
Solved! Go to Solution.
Solved by Jeff_M. Go to Solution.
I don't use VB, but I'll take a stab at it...
Dim noteId As ObjectId = NoteLabel.Create(ptLocation, styleId, markerId)
Where ptLocation is a valid 3dPoint, styleId is the ObjectId of the NoteLabelStyle to use, and markerId is the ObjectId of the MarkerStyle to use.
Or:
Dim noteId As ObjectId = NoteLabel.Create(db, ptLocation)
Where db is the Database to add the label to and ptLocation is a valide 3dPoint. This will use the default styles set in the AddGeneralNoteLabel command settings.
This might be useful to you as well :
Thanks,
Thanks!
NoteLable! I had the word General stuck in my head and couldn’t find it.
So the following code works great:
Dim pt3D As Point3d = New Point3d(5, 10, 0)
Dim NoteId As ObjectId = Autodesk.Civil.DatabaseServices.NoteLabel.Create(m_Database, pt3D)
But as soon as I use a CogoPoint X and Y, it works BUT when you try to drag the NoteLabel AutoCAD crashes. Is it me?
Dim pointId As ObjectId = Nothing
pointId = CogoPoints.GetPointByPointNumber(1)
Dim cogoPoint As CogoPoint = TryCast(pointId.GetObject(OpenMode.ForRead), CogoPoint)
Dim pt3D As Point3d = New Point3d(cogoPoint.Easting, cogoPoint.Northing, 0)
Dim NoteId As ObjectId = Autodesk.Civil.DatabaseServices.NoteLabel.Create(m_Database, pt3D)
Are you committing your Transaction? As I mentioned, I don't use VB, but the following test in C# works fine for me.
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using Autodesk.Civil.ApplicationServices; using Autodesk.Civil.DatabaseServices; using AcDb = Autodesk.AutoCAD.DatabaseServices; [CommandMethod("Notetest")] public void noyetest() { Document doc = Application.DocumentManager.MdiActiveDocument; AcDb.Database db = doc.Database; Editor ed = doc.Editor; PromptEntityOptions eOpts = new PromptEntityOptions("Select CogoPoint:"); eOpts.SetRejectMessage("...not a Cogo Point, try again!"); eOpts.AddAllowedClass(typeof(CogoPoint), true); PromptEntityResult eRes = ed.GetEntity(eOpts); while (eRes.Status == PromptStatus.OK) { using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) { CogoPoint pt = (CogoPoint)eRes.ObjectId.GetObject(AcDb.OpenMode.ForRead); AcDb.ObjectId noteId = NoteLabel.Create(db, new Autodesk.AutoCAD.Geometry.Point3d(pt.Easting, pt.Northing, 0)); tr.Commit(); } eRes = ed.GetEntity(eOpts); } }
Glad I could help. BTW, I tested one version where I just passed the CogoPoint.Location as the 3dpoint and it worked as well.
If you find a response which answers your question, please be sure to accept the actual post which helped you as the solution. This will help future searchers quickly find answers to their own questions. Thanks!
I'm a noob who is still learning.
The drawing wasn’t corrupt.
The following code I found is my problem:
Dim oNoteLabel As Autodesk.Civil.DatabaseServices.Label = DirectCast(m_trans.GetObject(NoteId, OpenMode.ForWrite, True), Autodesk.Civil.DatabaseServices.Label) oNoteLabel.SetTextComponentOverride(oNoteLabel.GetTextComponentIds(0), "New Text")
Are you saying you found the problem and fixed it? Or you still have a problem and it's in that bit of code? At first glance it appears that code should work. You may try setting a variable to the ObjectId of the TextComponent, then passing that variable as the argument in the SetTextComponentOverride. This works for me (based on the same code previously posted):
while (eRes.Status == PromptStatus.OK) { using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) { CogoPoint pt = (CogoPoint)eRes.ObjectId.GetObject(AcDb.OpenMode.ForRead); AcDb.ObjectId noteId = NoteLabel.Create(db, new Autodesk.AutoCAD.Geometry.Point3d(pt.Easting, pt.Northing, 0)); NoteLabel lbl = (NoteLabel)noteId.GetObject(AcDb.OpenMode.ForWrite); AcDb.ObjectId txtcomponentId = lbl.GetTextComponentIds()[0]; lbl.SetTextComponentOverride(txtcomponentId, "MyTest"); tr.Commit(); } eRes = ed.GetEntity(eOpts); }
Not sure what's going on. It works but using AutoCAD if you try to drag the lable AutoCAD crashes.
<CommandMethod("TESTING123")> _ Public Sub TESTING123() Dim m_doc As CivilDocument = Nothing Dim m_Database As Database = Nothing Dim m_Editor As Editor = Nothing Dim docCol As Autodesk.AutoCAD.ApplicationServices.DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager m_Database = docCol.MdiActiveDocument.Database m_Editor = docCol.MdiActiveDocument.Editor m_doc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument() Dim CogoPoints As CogoPointCollection = m_doc.CogoPoints Using doclock As DocumentLock = docCol.MdiActiveDocument.LockDocument() Using m_trans As Transaction = m_Database.TransactionManager.StartTransaction() Dim pointId As ObjectId = CogoPoints.GetPointByPointNumber(1) Dim cogoPoint As CogoPoint = TryCast(pointId.GetObject(OpenMode.ForRead), CogoPoint) Dim pt3D As Autodesk.AutoCAD.Geometry.Point3d = New Point3d(cogoPoint.Easting, cogoPoint.Northing, 0) Dim NoteId As ObjectId = Autodesk.Civil.DatabaseServices.NoteLabel.Create(m_Database, pt3D) Dim lbl As NoteLabel = CType(NoteId.GetObject(OpenMode.ForWrite), NoteLabel) Dim txtcomponentId As ObjectId = lbl.GetTextComponentIds(0) lbl.SetTextComponentOverride(txtcomponentId, "MyTest") m_trans.Commit() End Using End Using End Sub
I've learned a few things in this exercise...First, if a TextComponent is a ReferenceText and you set the TextOverride, it doesn't display unless the Reference object is set. Second, the Create(db, text) method does NOT use the default style for the note label...instead, it uses the first style in the styles collection. To use a different style, be sure to either set it to the correct one once it is created, or use the other overload method which accepts the styleid to use.
Edoc, I tested your code in both 2013 & 2014. It works just fine in both, no crashing at all. Also tried Debug and Release builds. Do you have all of your references set to NOT CopyLocal?
All references are set to Copy Local False.
I always compile as Debug.
As a noob, you lost me regarding reference text. I’m using “MyTest”.
I ran the same code as before except after the declaration of lbl, I added:
lbl.StyleName = "FIELD"
Error is:
Violation Reading 0x0000 Exception at ea58d600h
Followed by the option to save my DWG for RECOVER and to send an Error Report to Autodesk.
Okay so I started a new DWG. Created point # 1 and a Gen. Note "FIELD".
And was able to drag with no problem.
In other words, it works great. Thanks for all the help.
For anyone else reading this post:
After pulling my hair out (all three of them) the same code would sometimes work and sometimes AutoCAD would crash when trying to drag a label.
I found that if I run AUDIT before and after the sub,AutoCAD does not crash.
<CommandMethod("TESTING123")> _ Public Sub TESTING123() Dim m_doc As CivilDocument = Nothing Dim m_Database As Database = Nothing Dim m_Editor As Editor = Nothing Dim docCol As Autodesk.AutoCAD.ApplicationServices.DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager m_Database = docCol.MdiActiveDocument.Database m_Editor = docCol.MdiActiveDocument.Editor m_doc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument() Dim CogoPoints As CogoPointCollection = m_doc.CogoPoints Using doclock As DocumentLock = docCol.MdiActiveDocument.LockDocument() Using m_trans As Transaction = m_Database.TransactionManager.StartTransaction() Dim pointId As ObjectId = CogoPoints.GetPointByPointNumber(1) Dim cogoPoint As CogoPoint = TryCast(pointId.GetObject(OpenMode.ForRead), CogoPoint) Dim pt3D As Autodesk.AutoCAD.Geometry.Point3d = New Point3d(cogoPoint.Easting, cogoPoint.Northing, 0) Dim NoteId As ObjectId = Autodesk.Civil.DatabaseServices.NoteLabel.Create(m_Database, pt3D) Dim lbl As NoteLabel = CType(NoteId.GetObject(OpenMode.ForWrite), NoteLabel) lbl.StyleName = "FIELD" Dim txtcomponentId As ObjectId = lbl.GetTextComponentIds(0) Dim s As String = "MyTest" lbl.SetTextComponentOverride(txtcomponentId, s) m_trans.Commit() End Using End Using End Sub