Ejemplo n.º 1
0
        public static int Execute(/*params string[] parameters*/)
        {
            ComApi.InwOpState3 oState = ComApiBridge.ComApiBridge.State;

            Document doc = Application.ActiveDocument;

            ModelItemCollection currSelectionColl = doc.CurrentSelection.SelectedItems;

            if (currSelectionColl.Count > 0)
            {
                foreach (ModelItem item in currSelectionColl.DescendantsAndSelf)
                {
                    if (item.HasGeometry)
                    {
                        ComApi.InwOaPath oPath = ComApiBridge.ComApiBridge.ToInwOaPath(item);

                        Transform3D tr = item.Transform;//TODO: учитывать уже добавленную трансформацию!!!
                        //tr.Factor()
                        Transform3DComponents transform3DComponents = tr.Factor();
                        //transform3DComponents.ScaleOrientation
                        Point3D center = item.Geometry.BoundingBox.Center;

                        double z               = center.Z;
                        double zTransformed    = z * 1.1;
                        double correctionTrans = (z - zTransformed) /*/ 2*/;

                        ComApi.InwOpSelection comSelectionOut =
                            ComApiBridge.ComApiBridge.ToInwOpSelection(new ModelItemCollection()
                        {
                            item
                        });

                        ComApi.InwLTransform3f3 oTrans1
                            = (ComApi.InwLTransform3f3)oState
                              .ObjectFactory(ComApi.nwEObjectType.eObjectType_nwLTransform3f, null, null);
                        //растяжение по Z
                        ComApi.InwLVec3f scale
                            = (ComApi.InwLVec3f)oState
                              .ObjectFactory(ComApi.nwEObjectType.eObjectType_nwLVec3f, null, null);
                        scale.SetValue(1, 1, 1.1);
                        //смещение по Z
                        ComApi.InwLVec3f trans
                            = (ComApi.InwLVec3f)oState
                              .ObjectFactory(ComApi.nwEObjectType.eObjectType_nwLVec3f, null, null);
                        trans.SetValue(0, 0, correctionTrans);

                        //ComApi.InwLRotation3f scaleOrientation
                        //    = (ComApi.InwLRotation3f)oState
                        //    .ObjectFactory(ComApi.nwEObjectType.eObjectType_nwLRotation3f, null, null);
                        //ComApi.InwLUnitVec3f axis
                        //    = (ComApi.InwLUnitVec3f)oState
                        //    .ObjectFactory(ComApi.nwEObjectType.eObjectType_nwLUnitVec3f, null, null);
                        //axis.SetValue(0, 0, 1);
                        //scaleOrientation.SetValue(axis, 0);

                        //ComApi.InwLRotation3f Rotation
                        //    = (ComApi.InwLRotation3f)oState
                        //    .ObjectFactory(ComApi.nwEObjectType.eObjectType_nwLRotation3f, null, null);
                        //Rotation.SetValue(axis, 0);

                        //oTrans1.factor(scale, scaleOrientation, Rotation, trans);
                        //double[] matrix = ConvertDoubleArray((Array)((object)oTrans1.Matrix));

                        //oTrans1.MakeScale(scale);
                        //double[] matrix1 = ConvertDoubleArray((Array)((object)oTrans1.Matrix));
                        //oTrans1.MakeTranslation(trans);
                        //double[] matrix2 = ConvertDoubleArray((Array)((object)oTrans1.Matrix));

                        oTrans1.SetMatrix(new double[]
                        {
                            1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1.1, 0, 0, 0, correctionTrans, 1
                        });


                        //oTrans1.MakeScale(scaleVec);
                        oState.OverrideTransform(comSelectionOut, oTrans1);
                    }
                }
            }

            return(0);
        }
Ejemplo n.º 2
0
        public override int Execute(params string[] parameters)
        {
            string documentspath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string fileoutpath   = documentspath + "\\" + "CoordinationModel.nwd";

            Document doc         = Autodesk.Navisworks.Api.Application.ActiveDocument;
            double   scalefactor = UnitConversion.ScaleFactor(Units.Meters, doc.Units);

            DocumentSelectionSets myselectionsets = doc.SelectionSets;
            ModelItemCollection   clipbox         = new ModelItemCollection();

            foreach (SavedItem childItem in ((GroupItem)myselectionsets.RootItem).Children)
            {
                if (childItem.DisplayName == "hidden")
                {
                    SelectionSet oSet = childItem as SelectionSet;
                    doc.Models.SetHidden(oSet.ExplicitModelItems, false);
                    myselectionsets.Remove((GroupItem)myselectionsets.RootItem, oSet);
                }
                else if (childItem.DisplayName == "clipbox")
                {
                    SelectionSet oSet = childItem as SelectionSet;
                    clipbox.AddRange(oSet.ExplicitModelItems);
                }
            }

            Point3D selbbmin, selbbmax;

            if (parameters.Length == 0)
            {
                ModelItemCollection mysel = doc.CurrentSelection.SelectedItems;
                selbbmin = mysel.BoundingBox().Min;
                selbbmax = mysel.BoundingBox().Max;
            }
            else
            {
                fileoutpath = @parameters[0];
                selbbmin    = new Point3D(Convert.ToDouble(parameters[1].Replace("neg", "-")) * scalefactor, Convert.ToDouble(parameters[2].Replace("neg", "-")) * scalefactor, Convert.ToDouble(parameters[3].Replace("neg", "-")) * scalefactor);
                selbbmax    = new Point3D(Convert.ToDouble(parameters[4].Replace("neg", "-")) * scalefactor, Convert.ToDouble(parameters[5].Replace("neg", "-")) * scalefactor, Convert.ToDouble(parameters[6].Replace("neg", "-")) * scalefactor);
            }

            if (selbbmin.IsOrigin && selbbmax.IsOrigin)
            {
                doc.SaveFile(fileoutpath);
                return(0);
            }

            string        logfile = documentspath + "\\" + Path.GetFileName(fileoutpath) + ".txt";
            List <string> log     = new List <string>();
            //log.Add("scalefactor###" + scalefactor);
            //selbbmin = new Point3D(-7.000, 14.000, 17.000);
            //selbbmax = new Point3D(-5.000, 16.000, 19.000);

            Point3D  mid       = selbbmin.ToVector3D().Add(selbbmax).ToVector3D().Divide(2).ToPoint3D();
            Vector3D diff      = new Point3D(1, 1, 1).ToVector3D();
            double   deltaval  = 0.01;
            Vector3D delta     = new Point3D(deltaval, deltaval, deltaval).ToVector3D();
            double   clearance = 0;

            while (diff.X > 0 && diff.Y > 0 && diff.Z > 0)
            {
                clearance += deltaval;
                selbbmax   = selbbmax.Subtract(delta);
                selbbmin   = selbbmin.Add(delta);
                diff       = selbbmax.ToVector3D().Subtract(selbbmin.ToVector3D());
            }

            Transform3D           EndTransform         = new Transform3D();
            Transform3DComponents Transform3DComponent = EndTransform.Factor();

            Transform3DComponent.Scale = selbbmax.ToVector3D().Subtract(selbbmin.ToVector3D());
            Rotation3D ScaleOrientation = new Rotation3D(new UnitVector3D(0, 0, 1), 0);

            Transform3DComponent.Rotation    = ScaleOrientation;
            Transform3DComponent.Translation = mid.ToVector3D();
            EndTransform = Transform3DComponent.Combine();

            doc.Models.OverridePermanentTransform(clipbox, EndTransform, true);

            try
            {
                DocumentClash      documentClash  = doc.GetClash();
                DocumentClashTests myClashTests   = documentClash.TestsData;
                ClashTest          myClashTestorg = myClashTests.Tests[0] as ClashTest;
                ClashTest          myClashTest    = myClashTestorg.CreateCopy() as ClashTest;
                myClashTest.TestType  = ClashTestType.Clearance;
                myClashTest.Tolerance = Math.Pow((3 * Math.Pow(clearance, 2)), 0.5);
                myClashTest.Status    = ClashTestStatus.New;
                myClashTests.TestsEditTestFromCopy(myClashTestorg, myClashTest);
                myClashTests.TestsRunTest(myClashTestorg);
                myClashTest = myClashTests.Tests[0] as ClashTest;
                ModelItemCollection myClashingElements = new ModelItemCollection();
                for (var i = 0; i < myClashTest.Children.Count; i++)
                {
                    ClashResult myClash = myClashTest.Children[i] as ClashResult;
                    myClashingElements.AddRange(myClash.Selection2);
                }

                myClashingElements.Invert(doc);
                doc.Models.SetHidden(myClashingElements, true);

                SelectionSet allhidden = new SelectionSet(myClashingElements);
                allhidden.DisplayName = "hidden";
                myselectionsets.AddCopy(allhidden);

                myClashTests.TestsClearResults(myClashTest);
                //clipbox.AddRange((doc.SelectionSets.RootItem.Children[0] as SelectionSet).GetSelectedItems());
                doc.Models.ResetPermanentTransform(clipbox);
                doc.SaveFile(fileoutpath);
            }
            catch (Exception e) { log.Add(e.Message + "###" + e.StackTrace); }
            finally { File.WriteAllText(logfile, string.Join("\r\n", log)); }

            return(0);
        }