【更新中】Plant 3D中的自定义Python脚本 | Custom Python Scripts for AutoCAD Plant 3D

【更新中】Plant 3D中的自定义Python脚本 | Custom Python Scripts for AutoCAD Plant 3D

李榕华|Ronghua.LI
Advisor Advisor
10,126 次查看
13 条回复
1 条消息(共 14 条)

【更新中】Plant 3D中的自定义Python脚本 | Custom Python Scripts for AutoCAD Plant 3D

李榕华|Ronghua.LI
Advisor
Advisor
已接受的解答 (1)
10,127 次查看
13 条回复
回复 (13)
2 条消息(共 14 条)

李榕华|Ronghua.LI
Advisor
Advisor
已接受的解答

Custom Python Scripts for AutoCAD Plant 3D – Part 1

By David Wolfe (Contributor)

In this series of posts, we will cover developing custom python scripts for AutoCAD Plant 3D.

Along with learning about the development environment, we will cover troubleshooting, template scripts, and deployment. The original AU course covering scripts and Plant 3D is available here.

This blog article is going to assume some familiarity with Python. There are a lot of great resources for learning python on the internet. To get started learning the language syntax I suggest using Code Academy.

This introductory article will start off by covering the basics of:

Description of the development process

Programs are necessary for development

Description of script types

 

 

AutoCAD Plant 3D的自定义Python脚本-第1部分

作者:David Wolfe

在本系列文章中,我们将介绍为AutoCAD Plant 3D开发自定义python脚本。

在学习开发环境的同时,我们还将介绍故障排除、模板脚本和部署。此处提供涵盖脚本和Plant 3D的原始AU课程。

(原文链接会提示不安全,我把文档上传到这个楼层的附件里。)

这篇博客文章假设您对Python有一定的了解。互联网上有很多学习python的好资源。为了开始学习语言语法,我建议使用Code Academy

(我推荐个国内学习Python基础语法的网站:www.runoob.com 。)

这篇介绍性文章将首先介绍以下基本知识:

  • 开发流程
  • 开发计划
  • 脚本类型

 

 


李榕华

13489140049@qq.com




3 条消息(共 14 条)

李榕华|Ronghua.LI
Advisor
Advisor

Development Process

Unlike typical development done with .Net languages or even normal Python scripts, there is no integrated IDE for compiling, testing, and debugging. The compiling is done by running a special command in Plant 3D, and then the scripts are loaded into memory for the AutoCAD session. Since the only way to test changes is to start a new AutoCAD session, the process of troubleshooting The steps in the development process are:

  • Define function(s) for modeling the custom part
  • Define the metadata header
  • Test the script
  • Create the snapshots
  • Build a script package (if necessary)
  • Deploy the content

Through the rest of the articles, we’ll break down these steps even further so that you have an accurate understanding of the entire process. Along the way we will also develop a few custom scripts to help get you started.

 

 

开发流程

与使用.Net语言甚至普通Python脚本进行的典型开发不同,没有用于编译、测试和调试的集成IDE。编译是通过在Plant 3D中运行一个特殊命令来完成的,然后将脚本加载到内存中以用于AutoCAD会话。由于测试更改的唯一方法是启动新的AutoCAD会话,因此故障排除过程

(这里提到了一个非常重要的知识点,对Python内容进行修改之后,需要重启Plant 3D才能生效。)
开发过程中的步骤包括:

  • 定义自定义零件建模的功能
  • 定义元数据头
  • 测试脚本
  • 创建快照
  • 构建脚本包(如果需要)
  • 部署内容

在剩下的文章中,我们将进一步细分这些步骤,以便您准确地理解整个过程。在此过程中,我们还将开发一些自定义脚本来帮助您开始。

 

 


李榕华

13489140049@qq.com




4 条消息(共 14 条)

李榕华|Ronghua.LI
Advisor
Advisor

Development Programs

Due to the prevalence of Python, there are many good free editors. Since we are not looking for an IDE (Integrated Development Environment), any of the text editors that highlight the Python syntax will work well. I’ve used Komodo Edit, but my preference for text editors is TotalEdit Pro (now free). Again, the main job here is just to color the text. All of the testing of the code will be done in AutoCAD Plant, and there is no debugger to step through.

When building scripts for Equipment, you will need to have a zip program. My personal favorite is 7-zip, as it integrates well with the Windows Explorer right-click menu.

With xml files, you can use Foxe which offers a customizable tree view of the xml beside the xml content with syntax highlighting. Here’s a quick article showing how to customize the tree.

 

 

开发计划

由于Python的流行,有许多优秀的免费编辑器。由于我们不是在寻找IDE(集成开发环境),因此任何语法高亮的Python文本编辑器都能很好地工作。我使用过Komodo Edit,但我对文本编辑器的偏好是TotalEdit Pro(现在免费)。(我使用的是vs code同样,这里的主要工作只是给文本上色。所有代码测试都将在AutoCAD Plant 3D中完成,并且没有调试器可供调试。

在为设备构建脚本时,您需要有一个解压缩程序。我个人最喜欢的是7-zip,因为它与Windows资源管理器右键单击菜单集成得很好。(我使用的是360压缩国际版。本楼层附件我会放上截至2022.10.04最新的1.0.0.1041版本。)

对于*.xml文件,您可以使用Foxe,它在xml内容旁边提供了一个可自定义的xml树视图,并带有语法高亮显示。下面是一篇介绍如何自定义树形菜单的文章。(我把这活也丢给上文提到的vs code来干了。)

 

 


李榕华

13489140049@qq.com




5 条消息(共 14 条)

李榕华|Ronghua.LI
Advisor
Advisor

Visual Studio Code相关拓展

vs code官方网址:https://code.visualstudio.com/ 

百度百科地址:Visual Studio Code —— 百度百科 

节选了一下介绍:

Visual Studio Code(简称“VS Code”)是Microsoft在2015年4月30日Build开发者大会上正式宣布一个运行于 Mac OS X、Windows和 Linux 之上的,针对于编写现代Web和云应用的跨平台源代码编辑器,可在桌面上运行,并且可用于Windows,macOS和Linux。它具有对JavaScript,TypeScript和Node.js的内置支持,并具有丰富的其他语言(例如C++,C#,Java,Python,PHP,Go)和运行时(例如.NET和Unity)扩展的生态系统。

 

 

推荐几个我自己比较喜欢的插件:

Chinese (Simplified) (简体中文) Language Pack for Visual Studio Code 

此中文(简体)语言包为 VS Code 提供本地化界面。

 

Notepad++ keymap 

This extension ports popular Notepad++ keyboard shortcuts to Visual Studio Code.

此扩展将Notepad++的常用键盘快捷方式移植到Visual Studio Code。

因为我自己原来是使用Notepad++,然后换用vs code。相对来说比较熟悉Notepad++的快捷键,而且vs code的快捷键比较麻烦。

 

 


李榕华

13489140049@qq.com




6 条消息(共 14 条)

李榕华|Ronghua.LI
Advisor
Advisor

Script Types

Plant 3D uses scripts in a couple different ways, for equipment and also for catalog content. The scripts get stored in special folders inside the shared content folder. The two special folders for scripts are:

  • C:\AutoCAD Plant 3D 20XX Content\CPak Common\equipment
  • C:\AutoCAD Plant 3D 20XX Content\CPak Common\CustomScripts

The paths are above are the default locations. If you are using a shared network location, these folder should be located there. You may have to create the CustomScripts folder as it is not present by default.

If you are going to create an object that will connect to piping, and show as an end connection on the isometric (and is not a nozzle), you should create an equipment script. Any other type of script will be created as a custom script.

 

6a0167607c2431970b01b8d125aff6970c.png

 

 

脚本类型

Plant 3D以两种不同的方式使用脚本,用于设备和管路元件。脚本存储在共享内容文件夹内的特殊文件夹中。脚本的两个特殊文件夹是:

  • C:\AutoCAD Plant 3D 20XX Content\CPak Common\equipment
  • C:\AutoCAD Plant 3D 20XX Content\CPak Common\CustomScripts

上面的路径是默认位置。如果您使用的是共享网络位置,则这些文件夹应位于该位置。您可能需要创建CustomScripts文件夹,因为默认情况下它不存在。
如果要创建将连接到管道的对象,并在等轴测图上显示为端点连接(而不是管嘴),则应创建设备脚本。任何其他类型的脚本都将创建为自定义脚本。

 

 


李榕华

13489140049@qq.com




7 条消息(共 14 条)

80944291
Advocate
Advocate

你好,请教一个问题,在网上找到一个关于8字盲板的代码如下:

# Write Metadata Section
@Anonymous(Group="SpectaleBlind ", TooltipShort="Spectacle Blind", TooltipLong="Spectacle Blind (WF)", FirstPortEndtypes="WF", LengthUnit="in", Ports=2)
@group("MainDimensions")
@Anonymous(D1=LENGTH, TooltipLong="Outside Diameter of Plate")
@Anonymous(D2=LENGTH, TooltipLong="Inside Diameter of Plate")
@Anonymous(W=LENGTH, TooltipLong="Width of Web")
@Anonymous(L=LENGTH, TooltipLong="Centerline Distance")
@Anonymous(H=LENGTH, TooltipLong="Thickness of Plate")
@Anonymous(OF=LENGTH0)
@Anonymous(BD=LENGTH, TooltipLong="Hole Size")
@Anonymous(ST=INT, TooltipLong="Status")
# Shape Script
def SPECBLIND(s, D1 = 8.625, D2 = 6.625, W = 3.0, L = 9.5, H = 0.5, OF=-1, BD = 0.875, ST = 0, ID = 'SPECBLIND', **kw):

我想知道怎么操作才能把这个Python代码变成一个可以用的元件

谢谢

8 条消息(共 14 条)

WeTanks
Mentor
Mentor

你好啊,

你这个工作量有点大了,估计得让版主面授了。

We.Tanks

EESignature

A couple of Fusion improvement ideas that could your vote/support:
図面一括印刷

9 条消息(共 14 条)

李榕华|Ronghua.LI
Advisor
Advisor

这代码不完整,还没到样子部分,就没了。

完整的应该是这样的结构,你参考一下:

https://forums.autodesk.com/t5/autocad-plant-3d-chan-pin-ji-shu-ying-yong-tao-lun-qu/wan-mei-fa-lan-...


李榕华

13489140049@qq.com




10 条消息(共 14 条)

SCCYCN
Participant
Participant

柠神 设备用的脚本 是用什么软件打开呢 也可以用记事本打开吗

0 个赞
11 条消息(共 14 条)

李榕华|Ronghua.LI
Advisor
Advisor

@SCCYCN  已写:

柠神 设备用的脚本 是用什么软件打开呢 也可以用记事本打开吗


指的是peqx文件?


李榕华

13489140049@qq.com




0 个赞
12 条消息(共 14 条)

SCCYCN
Participant
Participant
嗯 对的 还望指点
0 个赞
13 条消息(共 14 条)

李榕华|Ronghua.LI
Advisor
Advisor

@SCCYCN  已写:
嗯 对的 还望指点

把peqx后缀名改成zip,就能用解压缩软件打开了。


李榕华

13489140049@qq.com




14 条消息(共 14 条)

SCCYCN
Participant
Participant

感谢🙏

0 个赞