using SolidWorks.Interop.cosworks;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Activities.Expressions;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace lab7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public dynamic Study;
        public dynamic COSMOSWORKS;
        public List<object> nodeList = new List<object>();
        public int errorCode1;
        public object[] stress;
        public object[] strain;
        public object[] displacement;

        public string name = "StudyStatic-1";
        public bool flag = false;

        ModelDoc2 doc;

        // подключение к NAME_1
        public object getSolidWorksActiveApp()
        {
            //Получаем активное(уже запущенное) приложение NAME_1
            object sldApp = null;

            try
            {
                sldApp = Marshal.GetActiveObject("SldWorks.Application");
            }
            catch
            {
                MessageBox.Show("Не удалось открыть SolidWorks либо найти открытое приложение");
                return null;
            }

            return sldApp;
        }

        // открытие детали
        public object openDoc(object swApp, string path)
        {
            SldWorks app = swApp as SldWorks;
            int fileError = default(int);
            int fileWarning = default(int);
            //Open doc
            ModelDoc2 swDoc = app.OpenDoc6(
                path,
                (int)swDocumentTypes_e.swDocPART,
                (int)swOpenDocOptions_e.swOpenDocOptions_Silent,
                null,
                ref fileError,
                ref fileWarning
            );

            if (swDoc != null)
            {
                //Set the working directory to the document directory
                string pathName = swDoc.GetPathName();
                app.SetCurrentWorkingDirectory(pathName.Substring(0, pathName.LastIndexOf("\\")));
            }

            doc = app.ActiveDoc;
            //открыть деталь
            //если нет открытого чертежа
            if (app.ActiveDoc == null)
            {
                //создать и открыть
                doc