Community
Civil 3D Customization
Welcome to Autodesk’s AutoCAD Civil 3D Forums. Share your knowledge, ask questions, and explore popular AutoCAD Civil 3D Customization topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Copy assembly with subassemblies using C#

1 REPLY 1
Reply
Message 1 of 2
marcelwg75
531 Views, 1 Reply

Copy assembly with subassemblies using C#

Hello,

 

I try to make a copy of an assembly using .Clone. I do get a copy, but it seems like the subassemblies are not copied, but are still under the copied assembly. Is there a way to copy a assembly with subassemlies? Or do I have to add an Assembly to the collection and then copy the subassemblies?

 

using (Transaction tsa = CadApp.Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
            {
                ObjectId myassemblyId2 = doc.AssemblyCollection[0];
                Assembly myassembly2 = tsa.GetObject(myassemblyId2, OpenMode.ForWrite) as Assembly;
                ed.WriteMessage("Assembly name: {0}\n", myassembly2.Name);



                // Open the Block table for read
                BlockTable acBlkTbl;
                acBlkTbl = tsa.GetObject(acCurDb.BlockTableId,
                                                OpenMode.ForRead) as BlockTable;

                // Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = tsa.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;

                // Create a copy of the assembly
                Assembly dd = myassembly2.Clone() as Assembly;
                dd.Name = "copy of assembly";
                

                // Create a matrix and move the assembly using a vector from (0,0,0) to (-50,0,0)
                Point3d acPt3d = new Point3d(0, 0, 0);
                Vector3d acVec3d = acPt3d.GetVectorTo(new Point3d(-50, 0, 0));

                dd.TransformBy(Matrix3d.Displacement(acVec3d));

                // Add the cloned assembly
                acBlkTblRec.AppendEntity(dd);
                tsa.AddNewlyCreatedDBObject(dd, true);


                // Save the new object to the database
                tsa.Commit();

  

1 REPLY 1
Message 2 of 2
gleeuwdrent
in reply to: marcelwg75

Quite an old post, but I was facing the same issue. I solved it by manually copy the subassemblies to the cloned assembly:

 

foreach (var group in originalAssembly.Groups)
{
	int index = 0;
	Subassembly previous = null;
	foreach (ObjectId id in group.GetSubassemblyIds())
	{
		Subassembly subassembly = t.GetObject(id, OpenMode.ForRead) as Subassembly;
		if (index == 0)
		{
			var newGroup = assemblyClone.CopySubassembly(subassembly.ObjectId);
			var newSub = newGroup.GetSubassemblyIds()[0].GetObject(OpenMode.ForRead) as Subassembly;
			previous = newSub;
		}
		else
		{
			ObjectId newSubId = assemblyClone.CopySubassembly(subassembly.ObjectId, previous.Points[subassembly.PointIndexHookTo]);
			var newSub = newSubId.GetObject(OpenMode.ForRead) as Subassembly;
			previous = newSub;

		}
		index++;
	}
}

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


 

Autodesk Design & Make Report