Ejemplo n.º 1
0
        public static void Create(ISldWorks app, ModelDoc2 mainModel)
        {
            string drawingTemplate = app.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplateDrawing);
            ModelDoc2 designProject = app.NewDocument(drawingTemplate, (int)swDwgPaperSizes_e.swDwgPaperBsize, 0, 0);
            DrawingDoc designProjectDrawing = (DrawingDoc)designProject;
            int errors = 0;
            app.ActivateDoc2(designProject.GetTitle(), false, ref errors);
            designProjectDrawing.SetupSheet5("", (int)swDwgPaperSizes_e.swDwgPapersUserDefined, (int)swDwgTemplates_e.swDwgTemplateCustom, 1, 100, false, "a4 - iso.slddrt", 0.297, 0.21, "", true);
            var h = designProjectDrawing.CreateDrawViewFromModelView3(mainModel.GetPathName(), "*Изометрия", 0.1, 0.2, 0);

            List<OrderComponentData> orderData = new List<OrderComponentData>();

            object[] allComponents = ((AssemblyDoc)mainModel).GetComponents(true);
            foreach (Component2 component in allComponents)
            {
                ModelDoc2 currentModel = component.GetModelDoc2();
                string ip = GetCustomPropertyValue(currentModel, "IsProduct");
                if (ip == "Yes")
                {
                    orderData.Add(
                        new OrderComponentData()
                        {
                            Article = GetCustomPropertyValue(currentModel, "Articul"),
                            Name = GetCustomPropertyValue(currentModel, "Part_Name_spec"),
                            Width = GetCustomPropertyValue(currentModel, "Size1_spec"),
                            Depth = GetCustomPropertyValue(currentModel, "Size2_spec"),
                            Height = GetCustomPropertyValue(currentModel, "Size3_spec"),
                            Color1 = GetCustomPropertyValue(currentModel, "Color1"),
                            Color2 = GetCustomPropertyValue(currentModel, "Color2"),
                            Color3 = GetCustomPropertyValue(currentModel, "Color3"),
                            Color4 = GetCustomPropertyValue(currentModel, "Color4"),
                            Color5 = GetCustomPropertyValue(currentModel, "Color5"),
                            Color6 = GetCustomPropertyValue(currentModel, "Color6"),
                            Color7 = GetCustomPropertyValue(currentModel, "Color7")
                        });
                }
            }

            TableAnnotation table = designProjectDrawing.InsertTableAnnotation(0.01, 0.15, (int)swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft, orderData.Count + 1, 14);
            table.BorderLineWeight = 0;
            table.GridLineWeight = 0;

            #region Заполнение заголовков колонок таблицы

            table.set_Text(0, 0, "Поз.");
            table.set_Text(0, 1, "Артикул");
            table.set_Text(0, 2, "Наименование изделия");
            table.set_Text(0, 3, "Ширина");
            table.set_Text(0, 4, "Глубина");
            table.set_Text(0, 5, "Высота");
            table.set_Text(0, 6, "Цвет 1");
            table.set_Text(0, 7, "Цвет 2");
            table.set_Text(0, 8, "Цвет 3");
            table.set_Text(0, 9, "Цвет 4");
            table.set_Text(0, 10, "Цвет 5");
            table.set_Text(0, 11, "Цвет 6");
            table.set_Text(0, 12, "Цвет 7");
            table.set_Text(0, 13, "Кол-во");

            #endregion

            int i = 1;
            foreach (OrderComponentData productData in orderData)
            {
                table.set_Text(i, 0, i.ToString());
                table.set_Text(i, 1, productData.Article);
                table.set_Text(i, 2, productData.Name);
                table.set_Text(i, 3, productData.Width);
                table.set_Text(i, 4, productData.Depth);
                table.set_Text(i, 5, productData.Height);
                table.set_Text(i, 6, productData.Color1);
                table.set_Text(i, 7, productData.Color2);
                table.set_Text(i, 8, productData.Color3);
                table.set_Text(i, 9, productData.Color4);
                table.set_Text(i, 10, productData.Color5);
                table.set_Text(i, 11, productData.Color6);
                table.set_Text(i, 12, productData.Color7);
                i++;
            }

            table.SetColumnWidth(0, 0.013, (int)swTableRowColSizeChangeBehavior_e.swTableRowColChange_TableSizeCanChange);
            table.SetColumnWidth(1, 0.025, (int)swTableRowColSizeChangeBehavior_e.swTableRowColChange_TableSizeCanChange);
            table.SetColumnWidth(2, 0.07, (int)swTableRowColSizeChangeBehavior_e.swTableRowColChange_TableSizeCanChange);

            for (int j = 3; j < 14; j++)
            {
                table.SetColumnWidth(j, 0.02, (int)swTableRowColSizeChangeBehavior_e.swTableRowColChange_TableSizeCanChange);
            }

            designProject.ViewZoomtofit2();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add componect to active doc
        /// </summary>
        void AddComponectToActiveDoc(string partFileName)
        {
            ModelDocExtension swDocExt;
            AssemblyDoc       swAssy;
            string            tmpPath;
            ModelDoc2         tmpObj;
            bool          boolstat;
            Component2    swcomponent;
            Feature       matefeature;
            string        MateName;
            string        FirstSelection;
            string        SecondSelection;
            swMateAlign_e Alignment;
            string        strCompName;
            string        AssemblyTitle;
            string        AssemblyName;
            int           errors   = 0;
            int           warnings = 0;
            int           mateError;

            // Get title of assembly document
            AssemblyTitle = pDoc.GetTitle();

            // Split the title into two strings using the period (.) as the delimiter
            string[] strings = AssemblyTitle.Split(new Char[] { '.' });

            // Use AssemblyName when mating the component with the assembly
            AssemblyName = (string)strings[0];


            boolstat = true;

            string strCompModelname = null;

            strCompModelname = partFileName;

            // Because the component resides in the same folder as the assembly, get
            // the assembly's path, strip out the assembly filename, concatenate
            // the rest of the path to the component filename, and use this string to
            // open the component
            tmpPath = pDoc.GetPathName();
            //int idx;
            //idx = tmpPath.LastIndexOf(AssemblyTitle);
            string compPath;

            //tmpPath = tmpPath.Substring(0, (idx));
            //compPath = string.Concat(tmpPath, strCompModelname);
            compPath = strCompModelname;

            // Open the component
            tmpObj = (ModelDoc2)swApp.OpenDoc6(compPath, (int)swDocumentTypes_e.swDocPART, 0, "", ref errors, ref warnings);

            // Check to see if the file is read-only or cannot be found; display error
            // messages if either
            if (warnings == (int)swFileLoadWarning_e.swFileLoadWarning_ReadOnly)
            {
                MessageBox.Show("This file is read-only.");
                boolstat = false;
            }

            if (tmpObj == null)
            {
                MessageBox.Show("Cannot locate the file.");
                boolstat = false;
            }

            //Re-activate the assembly so that you can add the component to it
            pDoc   = (ModelDoc2)swApp.ActivateDoc2(AssemblyTitle, true, ref errors);
            swAssy = (AssemblyDoc)pDoc;


            // Add the component to the assembly document.
            // Currently only one option,
            // swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig,
            // works for adding a part using AddComponent5

            // The other options,
            // swAddComponentConfigOptions_e.swAddComponentConfigOptions_NewConfigWithAllReferenceModels
            // and swAddComponentConfigOptions_e.swAddComponentConfigOptions_NewConfigWithAsmStructure,
            // work only for adding assemblies using AddComponent5
            swcomponent = (Component2)swAssy.AddComponent5(strCompModelname, (int)swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig, "", false, "", -1, -1, -1);

            // Get the name of the component for the mate
            strCompName = swcomponent.Name2;

            // Create the name of the mate and the names of the planes to use for the mate
            MateName        = "top_coinc_" + strCompName;
            FirstSelection  = "Top@" + strCompName + "@" + AssemblyName;
            SecondSelection = "Front@" + AssemblyName;
            swDocExt        = (ModelDocExtension)pDoc.Extension;
            pDoc.ClearSelection2(true);

            // Select the planes for the mate
            boolstat = swDocExt.SelectByID2(FirstSelection, "PLANE", 0, 0, 0, true, 1, null, (int)swSelectOption_e.swSelectOptionDefault);
            boolstat = swDocExt.SelectByID2(SecondSelection, "PLANE", 0, 0, 0, true, 1, null, (int)swSelectOption_e.swSelectOptionDefault);

            // Add the mate
            matefeature = (Feature)swAssy.AddMate3((int)swMateType_e.swMateCOINCIDENT, (int)swMateAlign_e.swMateAlignALIGNED, false, 0, 0, 0, 0, 0, 0, 0,
                                                   0, false, out mateError);
            matefeature.Name = MateName;

            pDoc.ViewZoomtofit2();
        }