明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1058|回复: 6

civil 3d api创建道路

[复制链接]
发表于 2018-4-16 12:55 | 显示全部楼层 |阅读模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.DatabaseServices;
using Autodesk.Civil.DatabaseServices.Styles;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using Autodesk.Civil;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections;
using System.Text.RegularExpressions;
using System.Security;
using System.Drawing.Printing;
using Autodesk.AutoCAD.PlottingServices;
using Autodesk.AutoCAD.Interop;
using System.Drawing.Drawing2D;
using Autodesk.AutoCAD.LayerManager;
using DotNetARX;
//需要添加对cad com组件引用
using Autodesk.AutoCAD.Interop.Common; //需要添加对cad com组件引用
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using System.Management;

namespace ShenlongdhCadSystem
{
    public class AutocadEntity
    {

        #region 创建道路3d模型      
        public static string EntityTinSurface()
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            PromptEntityOptions opt = new PromptEntityOptions("\n选择一个曲面");
            opt.SetRejectMessage("对象必须是一个曲面\n");
            opt.AddAllowedClass(typeof(TinSurface), false);
            TinSurface align = null;
            using (Transaction trans = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
            {
                ObjectId alignID = ed.GetEntity(opt).ObjectId;
                if (alignID.Database != null)
                {
                    align = trans.GetObject(alignID, OpenMode.ForRead) as TinSurface;
                    alignID_1 = alignID;
                }
            }
            return align.Name;
        }
        public static string EntityAlignment()
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            PromptEntityOptions opt = new PromptEntityOptions("\n选择一个路线");
            opt.SetRejectMessage("对象必须是一个路线\n");
            opt.AddAllowedClass(typeof(Alignment), false);
            Alignment align = null;
            using (Transaction trans = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
            {
                ObjectId alignID = ed.GetEntity(opt).ObjectId;
                if (alignID.Database != null)
                {
                    align = trans.GetObject(alignID, OpenMode.ForRead) as Alignment;
                }
            }
            return align.Name;
        }

        public static string EntityProfile()
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            PromptEntityOptions opt = new PromptEntityOptions("\n选择纵断面设计线");
            opt.SetRejectMessage("对象必须是一个纵断面设计线\n");
            opt.AddAllowedClass(typeof(Profile), false);
            Profile align = null;
            using (Transaction trans = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
            {
                ObjectId alignID = ed.GetEntity(opt).ObjectId;
                if (alignID.Database != null)
                {
                    align = trans.GetObject(alignID, OpenMode.ForRead) as Profile;
                }
            }
            return align.Name;
        }
        /// <summary>
        /// 创建装配
        /// </summary>
        /// <param name="assemblyName">装配名称</param>
        public static void RodCrossection(string assemblyName)
        {
            SelectTfbgParameter();
            //在非模态下打开模型空间前要解锁
            DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
            // Point3d location = point3d();//
            Database db = Application.DocumentManager.MdiActiveDocument.Database;
            CivilDocument civilDoc = CivilApplication.ActiveDocument;
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            using (Transaction acTrans = acDoc.TransactionManager.StartTransaction())
            {
                //string atcFilePath = @"C:\Users\Administrator\AppData\Roaming\Autodesk\C3D 2014\chs\Support\ToolPalette\Palettes\Assemblies - Metric.atc";
                String atcFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Autodesk\C3D 2014\chs\Support\ToolPalette\Palettes\Assemblies - Metric.atc";
                string itemId = "D3216842-CBB4-4F66-AD83-7428F91746AF";
                Point3d location = ed.GetPoint("选择导入装配位置\n").Value;
                ObjectId assemblyId3 = civilDoc.AssemblyCollection.ImportAssembly(assemblyName, atcFilePath, itemId, location);
                ////////////////
                Assembly assembly = acTrans.GetObject(assemblyId3, OpenMode.ForWrite) as Assembly;
                foreach (var ss in civilDoc.SubassemblyCollection)
                {
                    Subassembly subassembly = acTrans.GetObject(ss, OpenMode.ForWrite) as Subassembly;
                    if (subassembly.Name.Equals("LaneSuperelevationAOR - (Left)"))//左侧行车道
                    {
                        #region MyRegion
                        foreach (var psd in subassembly.ParamsDouble)
                        {
                            if (psd.DisplayName == "12409")        //半幅路基宽度
                                psd.Value = Thenewfileoperations.Lujik / 2.0;
                            else
                                if (psd.DisplayName == "12481")        //路拱坡度
                                    psd.Value = Thenewfileoperations.Lujianhp;
                        }
                        #endregion
                    }
                    else
                        if (subassembly.Name.Equals("LaneSuperelevationAOR - (Right)"))//右侧行车道
                        {
                            #region MyRegion
                            foreach (var psd in subassembly.ParamsDouble)
                            {
                                if (psd.DisplayName == "12409")        //半幅路基宽度
                                    psd.Value = Thenewfileoperations.Lujik / 2.0;
                                else
                                    if (psd.DisplayName == "12481")        //路拱坡度
                                        psd.Value = Thenewfileoperations.Lujianhp;
                            }
                            #endregion
                        }
                        else
                            if (subassembly.Name.Equals("BasicSideSlopeCutDitch - (Left)"))//左侧路基边坡
                            {
                                #region MyRegion
                                foreach (var psd in subassembly.ParamsDouble)
                                {
                                    if (psd.DisplayName == "813")        //挖方边坡坡度
                                    {
                                        if (Wfpd == 0)
                                            psd.Value = 1;
                                        else
                                            psd.Value = 1.0 / Wfpd;
                                    }
                                    else
                                        if (psd.DisplayName == "815")        //填方边坡坡度
                                        {
                                            if (Tfzpd == 0)
                                                psd.Value = 1 / 1.5;
                                            else
                                                psd.Value = 1 / Tfzpd;
                                        }
                                        else
                                            if (psd.DisplayName == "817")//前坡度
                                                psd.Value = 2;
                                            else
                                                if (psd.DisplayName == "823")//后坡度
                                                    psd.Value = 2;
                                                else
                                                    if (psd.DisplayName == "858")//坡度限制3
                                                        psd.Value = 0.25;
                                                    else
                                                        if (psd.DisplayName == "821")//边沟底部宽度
                                                        {
                                                            if (Bgdk == 0)
                                                                psd.Value = 0.2;
                                                            else
                                                                psd.Value = Bgdk;
                                                        }
                                                        else
                                                            if (psd.DisplayName == "825")//前边沟深度
                                                            {
                                                                if (Bgsh == 0 && Bgpd == 0)
                                                                    psd.Value = 0.2;//沟深度0.4
                                                                else
                                                                    psd.Value = Bgsh * Bgpd;
                                                            }
                                                            else
                                                                if (psd.DisplayName == "819")//前边沟深度
                                                                {
                                                                    if (Bgsh == 0 && Bgpd == 0)
                                                                        psd.Value = 0.2;//沟深度0.4
                                                                    else
                                                                        psd.Value = Bgsh * Bgpd;
                                                                }
                                }
                                #endregion
                            }
                            else
                                if (subassembly.Name.Equals("BasicSideSlopeCutDitch - (Right)"))//右侧路基边坡
                                {
                                    #region MyRegion

                                    foreach (var psd in subassembly.ParamsDouble)
                                    {
                                        if (psd.DisplayName == "813")        //挖方边坡坡度
                                        {
                                            if (Wfpd == 0)
                                                psd.Value = 1;
                                            else
                                                psd.Value = 1.0 / Wfpd;
                                        }
                                        else
                                            if (psd.DisplayName == "815")        //填方边坡坡度
                                            {
                                                if (Tfypd == 0)
                                                    psd.Value = 1 / 1.5;
                                                else
                                                    psd.Value = 1 / Tfypd;
                                            }
                                            else
                                                if (psd.DisplayName == "817")//前坡度
                                                    psd.Value = 2;
                                                else
                                                    if (psd.DisplayName == "823")//后坡度
                                                        psd.Value = 2;
                                                    else
                                                        if (psd.DisplayName == "858")//坡度限制3
                                                            psd.Value = 0.25;
                                                        else
                                                            if (psd.DisplayName == "821")//边沟底部宽度
                                                            {
                                                                if (Bgdk == 0)
                                                                    psd.Value = 0.2;
                                                                else
                                                                    psd.Value = Bgdk;
                                                            }
                                                            else
                                                                if (psd.DisplayName == "825")//前边沟深度
                                                                {
                                                                    if (Bgsh == 0 && Bgpd == 0)
                                                                        psd.Value = 0.2;//沟深度0.4
                                                                    else
                                                                        psd.Value = Bgsh * Bgpd;
                                                                }
                                                                else
                                                                    if (psd.DisplayName == "819")//前边沟深度
                                                                    {
                                                                        if (Bgsh == 0 && Bgpd == 0)
                                                                            psd.Value = 0.2;//沟深度0.4
                                                                        else
                                                                            psd.Value = Bgsh * Bgpd;
                                                                    }
                                    }

                                    #endregion
                                }
                }
                acTrans.Commit();
            }
            docLock.Dispose();
        }
        static ObjectId alignID_1;
        /// <summary>
        /// 创建道路
        /// </summary>
        /// <param name="Rodname">道路名称</param>
        /// <param name="assemblyName">装配名称</param>
        public static void CmdCreateCorridor(string Rodname, string assemblyName)
        {

            //在非模态下打开模型空间前要解锁
            DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = Application.DocumentManager.MdiActiveDocument.Database;
            CivilDocument civilDoc = CivilApplication.ActiveDocument;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                ObjectId styleId = TinSurface.Create(HostApplicationServices.WorkingDatabase, "surfaceName");
                //创建一个新的道路模型ID
                ObjectId newCorridorId = civilDoc.CorridorCollection.Add(Rodname);
                Corridor corridor = trans.GetObject(newCorridorId, OpenMode.ForWrite) as Corridor;
                //获取当前打开第一条路线ID
                //ObjectId alignmentId = civilDoc.GetAlignmentIds()[0];
                //Alignment alignment = trans.GetObject(alignmentId, OpenMode.ForRead) as Alignment;
                ///////////////

                ObjectId alignmentId = new ObjectId();
                Alignment alignment = null;
                foreach (ObjectId surfaceId in civilDoc.GetAlignmentIds())
                {
                    alignmentId = surfaceId;
                    alignment = trans.GetObject(surfaceId, OpenMode.ForRead) as Alignment;
                    if (alignment.Name == Rod3dmy.alignment)
                        break;
                }
                //获取道路纵断面ID
                ObjectId profileId = alignment.GetProfileIds()[0];
                foreach (ObjectId surfaceId in alignment.GetProfileIds())
                {
                    profileId = surfaceId;
                    Autodesk.Civil.DatabaseServices.Entity entity = trans.GetObject(surfaceId, OpenMode.ForRead) as Autodesk.Civil.DatabaseServices.Entity;
                    if (entity.Name == Rod3dmy.ZdmProfile)
                        break;
                }
                TinSurface surface = trans.GetObject(styleId, OpenMode.ForRead) as TinSurface;
                // 创建基线
                Baseline baseline = corridor.Baselines.Add("New Baseline", alignmentId, profileId);
                //添加道路曲面
                corridor.CorridorSurfaces.Add(Rod3dmy.TriangulateSurface);
                //添加装配
                baseline.BaselineRegions.Add("ccc", assemblyName);
                foreach (ObjectId surfaceId in civilDoc.GetSurfaceIds())//删除非指定的曲面
                {
                    Autodesk.Civil.DatabaseServices.Surface surf = trans.GetObject(surfaceId, OpenMode.ForRead) as Autodesk.Civil.DatabaseServices.Surface;
                    if (surf.Name != Rod3dmy.TriangulateSurface)
                    {
                        surf.UpgradeOpen();
                        surf.Erase();
                    }
                }
                SubassemblyTargetInfoCollection subtargetinfocoll = corridor.GetTargets();
                for (int i = 0; i < subtargetinfocoll.Count; i++)
                {
                    SubassemblyTargetInfo subassemblytargetinfo = subtargetinfocoll[i];
                    if (subassemblytargetinfo.TargetType == SubassemblyLogicalNameType.Surface)
                    {
                        //ObjectIdCollection ids = new ObjectIdCollection();
                        //ids.Add(alignID_1);
                        //subassemblytargetinfo.TargetIds = ids;
                        subassemblytargetinfo.TargetIds = civilDoc.GetSurfaceIds();
                    }
                }
                //更新目标信息
                corridor.SetTargets(subtargetinfocoll);
                corridor.Rebuild();
                trans.Commit();
            }
            docLock.Dispose();
        }
        #endregion
    }
}


评分

参与人数 1明经币 +1 收起 理由
xgr + 1

查看全部评分

发表于 2022-5-13 12:54 | 显示全部楼层
civil 3d 最大的优点 ,就是可以不断的让使用者向领导申请更新、购买配置更高的电脑
回复 支持 1 反对 0

使用道具 举报

发表于 2018-4-27 12:54 | 显示全部楼层
civil 3d用的人很少,土方计算这块还是很好用的,支持楼主吧
发表于 2022-5-13 13:50 | 显示全部楼层
civil 3d 最大的优点 ,就是汇报的时候可以说用的是国外的软件
发表于 2022-10-14 09:42 | 显示全部楼层
学习了,谢谢分享。
发表于 2023-4-18 07:06 | 显示全部楼层
l来晚了,高版本2022 如何用?
发表于 2023-4-18 07:42 | 显示全部楼层
你们这帮评论的,不厚道,
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-25 08:55 , Processed in 1.099001 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表