Пример #1
0
        void StreamCollectMaterialsSample()
        {
            Database db     = GetDatabase();
            Editor   editor = GetEditor();

            editor.WriteMessage("* StreamCollectMaterials *\n");
            editor.WriteMessage("StreamCollectMaterials collects bodies of the specified materials from the entities pushed into the stream.\n");
            editor.WriteMessage("Pick some objects in the current drawing. The bodies with materials specifed will be highlighted. And bodies with the same material are displayed in one color.\n");

            StreamCollectMaterials stream = new StreamCollectMaterials(db);
            // StreamCollectMaterials needs the users to specify the materials they're interested in by setting the MaterialFilter property.
            // Here we add all the material ids in the current drawing to the filter collection.
            DictionaryMaterialDefinition dictMaterials = new DictionaryMaterialDefinition(db);

            stream.MaterialFilter = dictMaterials.Records;
            // To combine the bodies with the same material instead of traversing the body chain
            stream.CombineBodies = true;

            ObjectIdCollection ids = PickObjectSet();

            if (ids.Count == 0)
            {
                editor.WriteMessage("No object is picked\n");
                return;
            }

            TransactionManager tm = db.TransactionManager;

            using (Transaction trans = tm.StartTransaction())
            {
                stream.PushDisplayParameters(DictionaryDisplayConfiguration.GetStandardDisplayConfiguration(db), trans);

                foreach (ObjectId id in ids)
                {
                    Entity entity = trans.GetObject(id, OpenMode.ForRead) as Entity;
                    stream.Stream(entity);
                }

                stream.PopDisplayParameters();
                trans.Commit();
            }

            ids = stream.MaterialIds;
            int color = 1;

            foreach (ObjectId id in ids)
            {
                AecModeler.Body body = stream.GetBody(id);
                HighlightBody(body, color++);
            }
        }
    /// <summary>
    /// Create a material style and override the display properties of this style.
    /// Use Stylemanager to see the overrided display properties.
    /// </summary>
    public void DisplayPropertiesMaterialSample()
    {
        Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("====================================================");
        Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nDisplay Properties for Material Sample:\n");
        Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("====================================================\n");

        Database db = Application.DocumentManager.MdiActiveDocument.Database;

        //MaterialUtility.
        TransactionManager tm    = db.TransactionManager;
        Transaction        trans = tm.StartTransaction();
        Editor             ed    = Application.DocumentManager.MdiActiveDocument.Editor;

        Autodesk.Aec.DatabaseServices.Dictionary dict = new DictionaryMaterialDefinition(db);

        MaterialDefinition materialDefinition = null;

        try
        {
            string name        = "ACADotNetMaterialDefination";
            string description = "Material def created through .NET API";

            if (!dict.Has(name, trans))
            {
                materialDefinition             = dict.NewEntry() as MaterialDefinition;
                materialDefinition.Description = description;
                dict.AddNewRecord(name, materialDefinition);
                trans.AddNewlyCreatedDBObject(materialDefinition, true);
            }
            else
            {
                materialDefinition = trans.GetObject(dict.GetAt(name), OpenMode.ForWrite) as MaterialDefinition;
            }



            DisplayPropertiesMaterial materialProperties = new DisplayPropertiesMaterial();

            materialProperties.SubSetDatabaseDefaults(db);
            materialProperties.SetToStandard(db);

            ObjectId materialId = AddMaterial("wood");
            materialProperties.SurfaceRenderingMaterialId       = materialId;
            materialProperties.SectionRenderingMaterialId       = materialId;
            materialProperties.SectionedBodyRenderingMaterialId = materialId;
            DisplayComponentHatch displayComponentHatch = new DisplayComponentHatch();

            materialProperties.SurfaceHatch.HatchType   = HatchType.UserDefined;
            materialProperties.SurfaceHatch.PatternName = "NETAPITESTPatternName";
            materialProperties.SurfaceHatch.Angle       = 90.0;

            ObjectId dispMaterialId = Override.AddToOverrideExtensionDictionaryAndClose(materialDefinition, materialProperties);

            DisplayRepresentationManager displayManager = new DisplayRepresentationManager(db);
            ObjectIdCollection           ids            = displayManager.GetDisplayRepresentationIdsFromCurrentViewport(RXObject.GetClass(typeof(MaterialDefinition)));

            ObjectId idActiveDispRep = ids[0];

            OverrideDisplayProperties overrideProperties = new OverrideDisplayProperties();
            overrideProperties.ViewId            = idActiveDispRep;
            overrideProperties.DisplayPropertyId = dispMaterialId;
            materialDefinition.Overrides.Add(overrideProperties);

            trans.Commit();
            ed.WriteMessage("\n" +
                            "One new style for material \"ACADotNetMaterialDefination\"is created and the display properties are overrided.\n" +
                            "Use StyleManager to see the settings."
                            + "\n");
        }
        catch (System.Exception e)
        {
            trans.Abort();
            ed.WriteMessage("\n" + e.Message + "\n");
            return;
        }
    }