Author Topic: Civil 3d. Corridor targets from XRefs  (Read 256 times)

0 Members and 1 Guest are viewing this topic.

Skre

  • Mosquito
  • Posts: 1
Civil 3d. Corridor targets from XRefs
« on: January 12, 2023, 08:07:15 AM »
Hello community!

I want to automate one of my key worktask with .net:

I have a corridor builded in Civil 3D. For targets i use polylines in XRefs.
Those Xrefs comes from another users. Polylines in XRefs are fine structured (layers, colors and other stuff are allways the same). Ony their position could be changed.
Obvisiously, after user replace old file with new one all corridor targets vanish. Don't know exactly why, but i blame new guids of polylines in new file.

So, i want to create a code wtich will replace targets in a corridor automaticaly without opening file with corridors.
And yes, if i open file with corridors - it regens and all targets with XRefs are cleared, so i dont have any data to operate with.

Longstory short:
1. Start code from a new file
2. By opendialogs - acquire databases of File with Corridors, and files with XRefs i want to update
3. Find Corridors in "File with Corridors".
4. Acquire targets ids
5. Manipulate.

I faced a wall on 4th step.
Targets from XRefs got (0) id if i dont open a file. And because of it - i can't get further to compare if this XRef is one i need or not.

I assume, that i need to "activate" corridors somehow, but all workarounds i could find in internet - gived no results.

These (0) id from debug - on attached screenshot.

Code:
Code: [Select]
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
            DocumentCollection dc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
           

            Autodesk.AutoCAD.Windows.OpenFileDialog ofdOrig = new Autodesk.AutoCAD.Windows.OpenFileDialog("Corridor File", "", "", "DWG", Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.NoUrls);
            ofdOrig.ShowDialog();
            string OrigFilePath = ofdOrig.Filename;

            Database OrigDb = new Database(true,false);
            OrigDb.ReadDwgFile(OrigFilePath, FileOpenMode.OpenForReadAndAllShare, true,"");
           
            Autodesk.AutoCAD.Windows.OpenFileDialog ofdXrefs = new Autodesk.AutoCAD.Windows.OpenFileDialog("XRef Files", "", "", "DWG", Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple);           
            ofdXrefs.ShowDialog();
            string[] XrefFilePaths = ofdXrefs.GetFilenames();       

            if ((ofdOrig.Filename.Length> 1)&&((XrefFilePaths.Length>0)))
                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {                   
                    HostApplicationServices.WorkingDatabase = OrigDb; //without this one it will not even get SubassemblyTargetInfoCollection.
                    BlockTable OrigBT = (BlockTable)OrigDb.BlockTableId.GetObject(OpenMode.ForWrite);
                    ObjectIdCollection CorIds = new ObjectIdCollection();
                    ObjectIdCollection XrefsIds = new ObjectIdCollection();
                    foreach(ObjectId OrigBtrId in OrigBT)
                    {
                        BlockTableRecord OrigBtr = (BlockTableRecord)OrigBtrId.GetObject(OpenMode.ForRead);
                        foreach(ObjectId TMPId in OrigBtr)
                        {
                            if (TMPId.ObjectClass.DxfName == "AECC_CORRIDOR")
                                CorIds.Add(TMPId);                           
                        }
                       
                    }
                    foreach (ObjectId CorId in CorIds)
                    {
                        Corridor Cor = (Corridor)CorId.GetObject(OpenMode.ForWrite);                       
                        foreach (Baseline Bl in Cor.Baselines)
                        {
                            foreach (BaselineRegion Blr in Bl.BaselineRegions)
                            {
                                SubassemblyTargetInfoCollection stic = Blr.GetTargets();
                                foreach (SubassemblyTargetInfo sti in stic)
                                {
                                    foreach (ObjectId TId in sti.TargetIds)//here is my breakpoint where i took screenshot
                                    {
                                        if (TId.ObjectClass.DxfName == "LWPOLYLINE")
                                        {
                                            //So, it newer comes here, because (0) ObjectId has null ObjectClass.DxfName
                                            Autodesk.AutoCAD.DatabaseServices.Polyline Poly = (Autodesk.AutoCAD.DatabaseServices.Polyline)TId.GetObject(OpenMode.ForRead);
                                        }
                                    }
                                }
                            }
                        }
                    }
                   
                }

        }
« Last Edit: January 12, 2023, 08:12:49 AM by Skre »