use ModelDoc2::EditBalloonProperties→don't reattach balloon.
Description
This method edits the selected balloon's properties
Syntax (OLE Automation)
retval = ModelDoc2.EditBalloonProperties ( style, size, upperTextStyle, upperText, lowerTextStyle, lowerText )
Input: | (long) style | Style of balloon as defined in swBalloonStyle_e |
Input: | (long) size | Balloon size as defined in swBalloonFit_e |
Input: | (long) upperTextStyle | Upper-text style as defined in swDetailingNoteTextContent_e |
Input: | (BSTR) upperText | Text for the upper-text in this balloon |
Input: | (long) lowerTextStyle | Lower-text style as defined in swDetailingNoteTextContent_e |
Input: | (BSTR) lowerText | Text for the lower-text in this balloon |
Return: | (LPDISPATCH) retval | Pointer to the balloon being edited |
***************************************************************************
2010 SOLIDWORKS API Help - Insert Autoballoons Example (VBA) (AutoBalloon2) http://help.solidworks.com/2010/english/api/sldworksapi/insert_autoballoons_example_vb_autoballoon2_vb.htm
Follow code result is
File = D:\\360安全浏览器下载\\B\\Assem.SLDDRW
DetailingBalloonsDisplayWithBentLeader = False
DetailingBOMBalloonStyle = 1
DetailingBOMBalloonFit = 2
DetailingAutoBalloonLayout = 1
DetailingBalloonBentLeaderLength = 6.25 mm
Drawing View1
No balloons added.
code is
'---------------------------------------------
'
' Preconditions:
' (1) Drawing document is open.
' (2) Drawing view is selected.
'
' Postconditions: If autoballoons have not already been inserted in the
' drawing document and the drawing document is not lightweight,
' then autoballoons are inserted for each resolved component in the
' selected drawing view.
'
'----------------------------------------------
Sub main()
Const swDetailingBalloonLayout_Default As Long = -1
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim vNoteArr As Variant
Dim vNote As Variant
Dim swNote As SldWorks.Note
Dim swAnn As SldWorks.Annotation
Dim vAttachPos As Variant
Dim vAnnPos As Variant
Dim bRet As Boolean
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
Set swSelMgr = swModel.SelectionManager
Set swView = swSelMgr.GetSelectedObject5(1)
bRet = swDraw.ActivateView(swView.Name): Debug.Assert bRet
Debug.Print "File = " & swModel.GetPathName
' Get autoballoon settings
Debug.Print " DetailingBalloonsDisplayWithBentLeader = " & swModel.GetUserPreferenceToggle(swDetailingBalloonsDisplayWithBentLeader)
Debug.Print " DetailingBOMBalloonStyle = " & swModel.GetUserPreferenceIntegerValue(swDetailingBOMBalloonStyle)
Debug.Print " DetailingBOMBalloonFit = " & swModel.GetUserPreferenceIntegerValue(swDetailingBOMBalloonFit)
Debug.Print " DetailingAutoBalloonLayout = " & swModel.GetUserPreferenceIntegerValue(swDetailingAutoBalloonLayout)
Debug.Print " DetailingBalloonBentLeaderLength = " & swModel.GetUserPreferenceDoubleValue(swDetailingBalloonBentLeaderLength) * 1000# & " mm"
Debug.Print ""
Debug.Print " " & swView.Name
vNoteArr = swDraw.AutoBalloon2(swDetailingBalloonLayout_Default, False)
' Returns an empty array if:
' * Balloons already exist in any drawing view on any on sheet in
' the drawing document.
' * Drawing document is lightweight.
' Returns a note for each resolved component in the selected drawing view.
If IsEmpty(vNoteArr) Then
Debug.Print " No balloons added."
Exit Sub
End If
For Each vNote In vNoteArr
Set swNote = vNote
Set swAnn = swNote.GetAnnotation
vAttachPos = swNote.GetAttachPos
vAnnPos = swAnn.GetPosition
Debug.Print " Text = " & swNote.GetText
Debug.Print " Layer = " & swAnn.Layer
Debug.Print " AttachPos = (" & vAttachPos(0) * 1000# & ", " & vAttachPos(1) * 1000# & ", " & vAttachPos(2) * 1000# & ") mm"
Debug.Print " AnnPos = (" & vAnnPos(0) * 1000# & ", " & vAnnPos(1) * 1000# & ", " & vAnnPos(2) * 1000# & ") mm"
'bRet = swAnn.SetPosition(vAnnPos(0) + 0.01, vAnnPos(1), vAnnPos(2))
Next
End Sub
'--------------------------------------
SolidworksApi macros
Would you be preselecting the balloon and the place you want to re-attach it to?
Yes. How to re-attach it.
**************************************************
Text Balloon to Item Number Balloon Macro | SOLIDWORKS Forums https://forum.solidworks.com/message/580980
Option Explicit
Private strNextBalloonValue As String
Sub InsertBalloon()
Dim swApp As SldWorks.SldWorks
Dim swSelMgr As SelectionMgr
Dim swEntity As Entity
Dim newBalloon As Note
Dim swDwgDoc As ModelDoc2
Dim strCompName As String
Dim strCompNameAndPath As String
On Error GoTo Err_InsertBalloon
Set swApp = Application.SldWorks
Set swDwgDoc = swApp.ActiveDoc
If swDwgDoc Is Nothing Then
MsgBox "No active document so can't continue", vbExclamation + vbSystemModal, "Insert Balloon"
GoTo Exit_InsertBalloon
End If
If swDwgDoc.GetType <> swDocDRAWING Then
'not a Dwg so quit
MsgBox "Current document is not a Drawing so can't continue", vbExclamation + vbSystemModal, "Insert Balloon"
GoTo Exit_InsertBalloon
Else
'is a dwg
Set swSelMgr = swDwgDoc.SelectionManager
If swSelMgr.GetSelectedObjectCount2(0) <> 2 Then
'other than 2 objects selected - 1st is the view 2nd is the Entity (face,edge,etc.)
MsgBox "One Entity to be selected only before using this function", vbExclamation + vbSystemModal, "Insert Balloon"
GoTo Exit_InsertBalloon
Else
'2 objects selected (1st is the view 2nd is the face or edge of the Part)
Dim swSelType As swSelectType_e
swSelType = swSelMgr.GetSelectedObjectType3(2, 0)
Select Case swSelType
Case swSelFACES, swSelEDGES, swSelSILHOUETTES, swSelVERTICES
'is a Face, Edge, etc
Set swEntity = swSelMgr.GetSelectedObject6(2, 0)
'get the component which owns the entity
Dim swSelectedComponent As Component2
Set swSelectedComponent = swEntity.GetComponent
'get the name and path for checking later for Bought Out
strCompName = Mid(swSelectedComponent.Name, 1, InStrRev(swSelectedComponent.Name, "-") - 1)
strCompNameAndPath = swSelectedComponent.GetPathName
'get the model which defines the component
Dim swSelectedModel As ModelDoc2
Set swSelectedModel = swSelectedComponent.GetModelDoc2
'was the model already loaded?
Dim bLoadedByMe As Boolean
If swSelectedModel Is Nothing Then
'Get the document specification to load the model
Dim swDocSpec As DocumentSpecification
Set swDocSpec = swApp.GetOpenDocSpec(swSelectedComponent.GetPathName)
'you may not want the user to notice the model opening...
swApp.DocumentVisible False, swDocSpec.DocumentType
'open the model
Set swSelectedModel = swApp.OpenDoc7(swDocSpec)
'set yourself a reminder to close the model
bLoadedByMe = True
'make sure all document open visible after this
swApp.DocumentVisible True, swDocSpec.DocumentType
End If
If Left(strCompNameAndPath, 3) = "S:\" Or Left(strCompName, 3) = "BO_" Then
'Bought Out Part so start from 100
If strNextBalloonValue = "" Then
'1st time so start from 100
strNextBalloonValue = "100"
Else
'not the 1st time so increment number
If IsNumeric(strNextBalloonValue) Then
strNextBalloonValue = CStr(CInt(strNextBalloonValue) + 1)
End If
End If
strNextBalloonValue = InputBox("Balloon Value?", , strNextBalloonValue)
Set newBalloon = swDwgDoc.InsertBOMBalloon2(swBS_Circular, swBF_Tightest, swBalloonTextCustom, strNextBalloonValue, swBalloonTextCustom, "")
Else
'treat component as Lambert Part
'check your property here...
Dim strProp As String
strProp = swSelectedModel.CustomInfo2("", "LE_DWGNO")
If strProp = "" Then
'property missing
Set newBalloon = swDwgDoc.InsertBOMBalloon2(swBS_Underline, swBF_Tightest, swBalloonTextCustom, "**MISSING PROPERTY**", swBalloonTextCustom, "")
Else
'property exists
Set newBalloon = swDwgDoc.InsertBOMBalloon2(swBS_Underline, swBF_Tightest, swBalloonTextCustomProperties, "LE_DWGNO", swBalloonTextCustom, "")
Dim doubleQuote As String
doubleQuote = Chr(34)
newBalloon.PropertyLinkedText = "$PRPMODEL:" & doubleQuote & "LE_DWGNO" & doubleQuote
End If
End If
If bLoadedByMe Then
swApp.QuitDoc swSelectedModel.GetTitle
End If
Case Else
MsgBox "One Entity to be selected only before using this function", vbExclamation + vbSystemModal, "Insert Balloon"
GoTo Exit_InsertBalloon
End Select
End If
End If
Exit_InsertBalloon:
On Error Resume Next
swDwgDoc.ForceRebuild3 True
Set swApp = Nothing
Set swSelMgr = Nothing
Set swEntity = Nothing
Set newBalloon = Nothing
Set swSelectedComponent = Nothing
Set swSelectedModel = Nothing
Set swDocSpec = Nothing
Set swDwgDoc = Nothing
Exit Sub
Err_InsertBalloon:
MsgBox Err.Number & " (" & Err.Description & _
") in procedure InsertBalloon of modMacros", vbExclamation + vbSystemModal
Resume Exit_InsertBalloon
End Sub
**************************************************************
Reattach balloon using API | SOLIDWORKS Forums https://forum.solidworks.com/message/672842#comment-672842
2012 SOLIDWORKS API Help - Insert and Show BOM Table and BOM Balloon Example (VBA) http://help.solidworks.com/2012/english/api/sldworksapi/Insert_and_Show_BOM_Table_and_BOM_Balloon_Example_VB.htm
Add a balloon to a component in drawing | SOLIDWORKS Forums https://forum.solidworks.com/message/568282
How to move Arrow point of Note (balloon)? | SOLIDWORKS Forums https://forum.solidworks.com/message/498045
How do I get all position of balloon in the dra... | SOLIDWORKS Forums https://forum.solidworks.com/message/348900