Adding text in a drawing

Adding text in a drawing

kv5053545
Advocate Advocate
508 Views
3 Replies
Message 1 of 4

Adding text in a drawing

kv5053545
Advocate
Advocate

 

Hi, I have been working on adding text automatically in a .dwg file, but on some occasions the text is not seen because it is placed on the lines of the drawing and visually is lost, here my question is it possible to place the text on a white background using code?
This is the way I am adding the text in the drawing.

Sub Main()
     Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
     Dim oActiveSheet As Sheet = oDoc.ActiveSheet


     For Each oDrawingView As DrawingView In oActiveSheet.DrawingViews

         If oDrawingView.Name = "VIEW3" Then
             Dim oRefDoc As AssemblyDocument = oDrawingView.ReferencedDocumentDescriptor.ReferencedDocument
             Dim oAssDef As AssemblyComponentDefinition = oRefDoc.ComponentDefinition

             Dim oOcc As ComponentOccurrence
             For Each oOcc In oAssDef.Occurrences
                 Dim oSubOcc As ComponentOccurrence
                 For Each oSubOcc In oOcc.SubOccurrences
                     If oSubOcc.Name.Contains("Viewports") Then
                         Dim activeDoc As DrawingDocument = ThisApplication.ActiveDocument
                         Dim activeView As DrawingView = activeDoc.ActiveSheet.DrawingViews.Item(3)

                         Dim occWorkPoint As WorkPoint = GetWorkPointByName(oSubOcc, "WP_1")
                         Dim centerX As Double = activeView.Position.X - 0.56
                         Dim centerY As Double = activeView.Position.Y - 2
                         Dim centerX1 As Double = activeView.Position.X - 0.95

                         If Not occWorkPoint Is Nothing Then
                             Dim occWorkPointProxyObj As Object
                             oSubOcc.CreateGeometryProxy(occWorkPoint, occWorkPointProxyObj)
                             Dim occWorkPointProxy = TryCast(occWorkPointProxyObj, WorkPointProxy)

                             Dim xCoordUPP As Double = Math.Abs(occWorkPointProxy.Point.X) / 39.37
                             Dim yCoordUPP As Double = Math.Abs(occWorkPointProxy.Point.Z) / 39.37

                             xCoordUPP = Math.Round(xCoordUPP, 2)
                             yCoordUPP = Math.Round(yCoordUPP, 2)

                             Dim notePosition As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(centerX1, centerY)
                            
                             Dim noteText As String = "ID: " & iProperties.Value(oRefDoc.DisplayName, "Custom", "ID")
                            
                             Dim oGeneralNote As GeneralNote
                             oGeneralNote = oActiveSheet.DrawingNotes.GeneralNotes.AddFitted(notePosition, noteText)

                             Dim notePosition1 As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(centerX, yCoordUPP)
                             Dim noteText1 As String = iProperties.Value(oRefDoc.DisplayName, "Custom", "NumViewport")
                             Dim oGeneralNote1 As GeneralNote
                             oGeneralNote1 = oActiveSheet.DrawingNotes.GeneralNotes.AddFitted(notePosition1, noteText1)

                         End If
                     End If
                 Next
             Next
		 End if
     Next
 End Sub

 Function GetWorkPointByName(occ As ComponentOccurrence, workPointName As String) As WorkPoint
     Dim occWorkPoints As WorkPoints = GetWorkPoints(occ)
     If occWorkPoints IsNot Nothing Then
         For Each occWorkPoint As WorkPoint In occWorkPoints
             If occWorkPoint.Name = workPointName Then
                 Return occWorkPoint
             End If
         Next
     End If
     Return Nothing
 End Function

 Function GetWorkPoints(occ As ComponentOccurrence) As WorkPoints
     Try
         Dim asmDef As AssemblyComponentDefinition = TryCast(occ.Definition, AssemblyComponentDefinition)
         If Not asmDef Is Nothing Then Return asmDef.WorkPoints
         Return Nothing
     Catch
     End Try
 End Function

 

0 Likes
509 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @kv5053545.  After Line 46, where you then have access to the GeneralNote object that you just created, you can add more lines of code to change the background color of the note.  The GeneralNote object has the GeneralNote.BackgroundColor property that we can use for this, but of course you would have to have a 'Color' object to set as its value first.  You may already know this, but the Color object is 'transient' (temporary, just used for transferring an organized assembly of data from one place to another), and can be created with the TransientObjects.CreateColor method, where it wants you to specify 'Red, Green, Blue' and optionally 'Opacity' values.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 4

kv5053545
Advocate
Advocate

Hi @WCrihfield  , I implemented what you suggested but still the text does not appear with the white background, could you give me some guidance?
These are the lines of code that I added, I don't know if I am missing something.

                             Dim whiteColor As Color
                             whiteColor = ThisApplication.TransientObjects.CreateColor(255, 255, 255) 
                             oGeneralNote.BackgroundColor = whiteColor
							
								
                             oActiveSheet.Update()

 

 

0 Likes
Message 4 of 4

kv5053545
Advocate
Advocate
I was able to solve it, I was missing this line of code: oGeneralNote.UseBackgroundColor = True, thank you very much.