set parameters in subassembly

set parameters in subassembly

marcelwg75
Contributor Contributor
996 Views
2 Replies
Message 1 of 3

set parameters in subassembly

marcelwg75
Contributor
Contributor

I try to set parameters in a subassembly by C#. I followed https://knowledge.autodesk.com/support/autocad-civil-3d/learn-explore/caas/CloudHelp/cloudhelp/2017/...

but when I run the script the parameter is changed if I read the parameters, but the parameter in the subassembly itself it's not changed. How do I change a parameter of a subassembly in a corridor?

 

CivilDocument doc = CivilApplication.ActiveDocument;

           Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

           Editor _editor = Application.DocumentManager.MdiActiveDocument.Editor;

           using (Transaction ts = Application.DocumentManager.MdiActiveDocument.

               Database.TransactionManager.StartTransaction())

           {

               string corridorname = "COR_ALM_N_2016";

               //get the corrdior by name:

               ObjectId corridorId = doc.CorridorCollection[corridorname];

               Corridor corridor = ts.GetObject(corridorId, OpenMode.ForRead) as Corridor;

 

               //get the first applied subassembly in the first assembly in the first BaselineRegion:

              BaselineRegion baselineregion = corridor.Baselines[0].BaselineRegions[0];

               AppliedAssembly appliedassembly = baselineregion.AppliedAssemblies[0];

 

               AppliedSubassembly appliedsubassembly = appliedassembly.GetAppliedSubassemblies()[0];

               var adds = appliedassembly.GetAppliedSubassemblies();

               ed.WriteMessage("\nAantal subassemblies {0}\n", adds.Count);

 

 

 

               //get parameter key names:

               foreach (IAppliedSubassemblyParam parama in appliedsubassembly.Parameters)

               { ed.WriteMessage("\nParameter key name: {0} & {1} & {2}\n", parama.KeyName, parama.ValueAsObject, parama.ValueType); }

 

               foreach (var p in appliedsubassembly.Parameters)

               {

                   _editor.WriteMessage("Parameter information: name: {0}, value type: {1}\n", p.KeyName, p.ValueType);

                   Object vobj = p.ValueAsObject;

                   if (p.ValueType == typeof(System.String))

                   { _editor.WriteMessage("Parameter string value: {0}\n", (String)vobj); }

                   else if (p.ValueType == typeof(System.Double))

                   { _editor.WriteMessage("Parameter double value: {0}\n", (Double)vobj); }

                   else if (p.ValueType == typeof(System.Int32))

                   { _editor.WriteMessage("Parameter int32 value: {0}\n", (Int32)vobj); }

                   else if (p.ValueType == typeof(System.Boolean))

                   { _editor.WriteMessage("Parameter bool value: { 0}\n", (Boolean)vobj); }

 

                   // we should never get here:

                   else { _editor.WriteMessage("Unexpected type.\n"); }

               }

 

               // Check whether a parameter exists

               bool result = appliedsubassembly.Contains("Bteen");

               // Change a parameter’s value

               AppliedSubassemblyParam<double> param = appliedsubassembly.GetParameter<double>("Bteen");

               ed.WriteMessage("\nValue before {0}m dik {1}\n", param.KeyName, param.Value);

               param.Value = 4.0;

               appliedsubassembly.GetParameter<double>("Bteen").Value = 4.0;

 

               ed.WriteMessage("\nValue after {0}m dik {1}\n", param.KeyName, param.Value);

           }

0 Likes
997 Views
2 Replies
Replies (2)
Message 2 of 3

tyronebk
Collaborator
Collaborator

Did you commit your transaction? I don't see that happening in your example code.

 

PS - Using the Insert Code button or the <code> tag when inserting code makes it much easier to read.

0 Likes
Message 3 of 3

marcelwg75
Contributor
Contributor

I think I solved it. With the code before I changed the parameter of a single APPLIED subassembly, that is one section on the baseline. And only the "value" and not the "designvalue" (thanks to SNOOP!).

 

This works for me (found it on

http://help.autodesk.com/view/CIV3D/2017/ENU/?guid=GUID-DD4C3865-E531-42D2-8C5A-E86495472951

 ObjectId myassemblyId2 = doc.AssemblyCollection["Assembly_ (2)"];
                Assembly myassembly2 = ts.GetObject(myassemblyId2, OpenMode.ForRead) as Assembly;
                _editor.WriteMessage("Assembly name: {0}\n", myassembly2.Name);

                
                foreach (AssemblyGroup assemblyGroup2 in myassembly2.Groups)
                {
                 _editor.WriteMessage("Subassemblies in Group {0}:\n", assemblyGroup2.Name);
                foreach (ObjectId subassemblyid2 in assemblyGroup2.GetSubassemblyIds())
                 {
                    Subassembly subassembly2 = ts.GetObject(subassemblyid2, OpenMode.ForWrite) as Subassembly;
                     _editor.WriteMessage("Subassembly: {0}\n\n---------\n", subassembly2.Name);

                     var MGRAA = subassembly2.ParamsDouble[9];
                     _editor.WriteMessage("ParamsDouble value = {0}\n\n---------\n", MGRAA.Value);
                        MGRAA.Value = 4.0;
                     corridor.Rebuild();
                     ts.Commit();
0 Likes