Set the Length of selected Magnetic Line in Solidworks drawing

Hello Everyone,
I would like to set the length of the selected magnetic lines.
So far I have wrote down as below

myDrawingSheet = swModel.GetCurrentSheet()
ArrMagneticLines = myDrawingSheet.GetMagneticLines()
Try 'remove all the mag line
tview = swModel.GetFirstView
While Not tview Is Nothing
For iu As Integer = 1 To 20
Dim bool As Boolean = swModel.Extension.SelectByID2("MagneticLine" & iu & "@" & tview.GetName2, "MAGNETLINE", 0, 0, 0, False, 0, Nothing, 0)
If bool Then
Dim instance As MagneticLine
Dim swm As SelectionMgr
swm = swModel.SelectionManager
'Dim jkj = swm.GetSelectedObjectCount2(0)
**instance = swm.GetSelectedObject5(swSelectType_e.SwSelMAGNETICLINES)**
If Not instance Is Nothing Then
instance.Length = 100
End If
End If
tview = tview.GetNextView
End While

Catch ex As Exception

End Try

But getting the magnetic line on this line returns “Nothing”

instance = swm.GetSelectedObject5(swSelectType_e.SwSelMAGNETICLINES)

Can anybody throw some light on this issue?

Thanks in Advance!

Can you try the following snippet in place of the one you have the error with? This checks to make sure the selected object is of the type you’re expecting before setting the instance object to the selected item.

    If swSelMan.GetSelectedObjectType3(1, -1) = swSelectType_e.swSelMAGNETICLINES Then
        Set instance = swSelMan.GetSelectedObject6(1, -1)
    End If

I think you may have mixed up the GetSelectedObject with GetSelectedObjectType method a bit.

Hi @BigCrazyAl
Thanks for the reply.
I have also tried your code and made sure that my selected object was swSelMAGNETICLINES
Still, it returns “Nothing”

Refactored it a little bit, this works for me. Keep in mind setting the length to 100 means 100 meters.

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swDraw As DrawingDoc
    Dim swSheet As sheet
    Dim swMagLine As MagneticLine
    Dim i As Integer
    Dim k As Integer
    Dim sheetNames As Variant
    Dim ArrMagneticLines As Variant
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    sheetNames = swDraw.GetSheetNames
    swModel.ForceRebuild3 True
    For i = 0 To UBound(sheetNames)
        Set swSheet = swDraw.sheet(sheetNames(i))
        ArrMagneticLines = swSheet.GetMagneticLines
        For k = 0 To UBound(ArrMagneticLines)
            Set swMagLine = ArrMagneticLines(k)
            swMagLine.Length = 100
        Next k
    Next i
End Sub

Hi @BigCrazyAl

Thanks. This code will modify the length of magnetic lines in all the views in the sheet.
I would like to change the length on a specific view only.

Magnetic lines appear to attach themselves to the sheet only. If I try to place on into a view and move the view, the lines don’t move with it.

Sorry, I don’t think there’s a way to do it by whether a specific view contains the line unless you use the bounds of the view to determine if the points of the magnetic line fall within them.

If the balloon is added and attached to the magnetic line, then if we move the view, then the magnetic line will also move automatically.

Thanks for trying to check out the solution.
I will try to do some work around.