Hide Table of Contents

Insert Magnetic Line Example (C#)

This example shows how to insert a magnetic line in a drawing sheet.

//----------------------------------------------------------------------------
// Preconditions:
// 1. Open public_documents\samples\tutorial\api\Assem20.slddrw.
// 2. Open the Immediate window.
//
// Postconditions: At each System.Diagnostics.Debugger.Break() statement,
// observe the Immediate window and drawing, then press F5:
// 1. Inserts a magnetic line.
// 2. Attaches two notes to the magnetic line.
// 3. Detaches one note from the magnetic line.

//
// NOTE: Because the drawing is used elsewhere, do not save changes.
// ---------------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
namespace InsertMagneticLine_CSharp.csproj
{
    
partial class SolidWorksMacro
    {

        
ModelDoc2 Part;
        
DrawingDoc swDraw;
        
Note swNote;
        
Annotation anno;
        
MathUtility MathUtil;
        
MathPoint StartPoint;
        
MathPoint EndPoint;
        
MathPoint NewLocation;
        
MagneticLine MagLine;
        
object startPointArray;
        
double[] startPointCoords = new double[3];
        
object endPointArray;
        
double[] endPointCoords = new double[3];
        
object vAnnPos;
        
Sheet myDrawingSheet;
        
bool boolstatus;

        
SelectionMgr swSelMgr;

        
public void Main()
        {
            Part = (
ModelDoc2)swApp.ActiveDoc;
            swDraw = (
DrawingDoc)Part;
            swSelMgr = (
SelectionMgr)Part.SelectionManager;

            boolstatus = Part.Extension.SelectByID2(
"DetailItem350@Drawing View1", "NOTE", 0.1999417828571, 0.1868875085714, 0, false, 0, null, 0);
            swNote = (
Note)swSelMgr.GetSelectedObject6(1, 0);

            MathUtil = (
MathUtility)swApp.GetMathUtility();

            startPointCoords[0] = 0.00792759673091;
            startPointCoords[1] = 0.01114535920683;
            startPointCoords[2] = 0;
            startPointArray = startPointCoords;
            StartPoint = (
MathPoint)MathUtil.CreatePoint((startPointArray));

            endPointCoords[0] = 0.2681801120698;
            endPointCoords[1] = 0.2077950154354;
            endPointCoords[2] = 0;
            endPointArray = endPointCoords;
            EndPoint = (
MathPoint)MathUtil.CreatePoint((endPointArray));

            myDrawingSheet = (
Sheet)swDraw.GetCurrentSheet();
            System.Diagnostics.
Debugger.Break();

            MagLine = myDrawingSheet.InsertMagneticLine(StartPoint, EndPoint);
            MagLine.EqualSpacing =
true;
            
Debug.Print("Number of magnetic lines on this sheet: " + myDrawingSheet.GetMagneticLinesCount());
            System.Diagnostics.
Debugger.Break();

            
Debug.Print("First note attached to magnetic line? " + MagLine.AddNote(swNote, 1E-06));
            
// If EqualSpacing is True then Position is ignored
            Debug.Print("Number of notes attached to the magnetic line: " + MagLine.GetNotesCount());
            System.Diagnostics.
Debugger.Break();

            
Debug.Print("Angle in radians of the magnetic line: " + MagLine.Angle);
            
Debug.Print("Length in meters of the magnetic line: " + MagLine.Length);

            boolstatus = Part.Extension.SelectByID2(
"DetailItem352@Drawing View3", "NOTE", 0.1944023085714, 0.1194905714286, 0, false, 0, null, 0);
            swNote = (
Note)swSelMgr.GetSelectedObject6(1, 0);
            
Debug.Print("Second note attached to magnetic line? " + MagLine.AddNote(swNote, 1E-06));
            
// This fails because note belongs to a different view
            anno = (Annotation)swNote.GetAnnotation();
            
Debug.Print("Number of notes attached to the magnetic line: " + MagLine.GetNotesCount());
            System.Diagnostics.
Debugger.Break();

            boolstatus = Part.Extension.SelectByID2(
"DetailItem351@Drawing View1", "NOTE", 0.1944023085714, 0.1194905714286, 0, false, 0, null, 0);
            swNote = (
Note)swSelMgr.GetSelectedObject6(1, 0);
            anno = (
Annotation)swNote.GetAnnotation();
            vAnnPos = anno.GetPosition();
            
Debug.Print("Second note attached to magnetic line? " + MagLine.AddNote(swNote, 1E-06));
            
// This succeeds because note belongs to same view as previously added note
            Debug.Print("Number of notes attached to the magnetic line: " + MagLine.GetNotesCount());
            System.Diagnostics.
Debugger.Break();

            
// Detach a note from the magnetic line
            NewLocation = (MathPoint)MathUtil.CreatePoint((vAnnPos));
            
Debug.Print("Note detached from magnetic line? " + MagLine.RemoveNote(swNote, NewLocation));
            
// Move the note to its original position
            Debug.Print("Number of notes attached to the magnetic line: " + MagLine.GetNotesCount());

        }

        
public SldWorks swApp;

    }

}



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Insert Magnetic Line Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:




x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2018 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.