Surpac与Python集成实战:超越原生功能的自动化与数据分析
在矿业数字化转型的实践中,一个核心矛盾日益凸显:专用矿业软件的深度与通用编程生态的广度,仿佛是两个难以交汇的宇宙。据不完全统计,全球超过60%的矿山规划软件依赖其封闭的脚本语言实现自动化,而与此同时,Python以其在数据分析、机器学习和科学计算领域超过80%的占有率,成为技术创新的首要语言。当这两个世界打破壁垒,深度融合时,会迸发出怎样的能量?对于全球数十万用户而言,Surpac 2025 Refresh 2版本带来的Python深度集成能力,正在彻底改写这一行业现状,让每一位矿业工程师都能解锁超越软件原生功能的无限可能。《升华洞察》推出《GEOVIA CONNECT 25X 实战》系列文章,以“矿业全流程价值实现”为主线,匹配矿山实际生产业务流,完成了从战略视野到落地执行的全链条覆盖。在此前的九篇文章中,我们依次拆解了AI在地质勘探中的实战应用、复杂矿体资源估算的核心优化方法、Surpac最新功能的落地技巧,打通了地质与采矿的协作壁垒。同时,我们完整覆盖了露天矿山从战略坑优化、详细设计到生产调度的全流程,也深入讲解了地下矿山采场优化与崩落法高效规划的核心技术,构建了完整的矿山数字化技术体系。作为本系列的压轴收官之作,本文内容来自GEOVIA CONNECT 25X全球技术大会上,达索系统GEOVIA资深技术专家Ash COLTON的实战演讲。Ash拆解了Surpac与Python深度集成的核心逻辑、三种执行方式、六大实战场景,彻底打通从基础操作到高阶自动化的全路径。揭示了如何用Python这把“万能瑞士军刀”,撬动Surpac这座“精密数控机床”的无限潜能。一、架构破壁:从TCL孤岛到Python大陆的桥梁搭建
今天我要和大家深入拆解的核心主题,就是如何在Surpac原生环境中,深度调用Python能力完成各项技术工作。本次分享的核心目标,是向大家完整展示在Surpac中调用Python的三种不同实现路径,帮大家彻底理解这套全新能力的底层逻辑与落地方法。在正式进入技术细节之前,我想先和大家简要梳理一下Surpac当前的原生能力边界,以及Surpac的底层架构与宏语言体系,帮大家搭建完整的认知框架。很多工程师常年使用Surpac,却很少关注它的底层架构逻辑,而这恰恰是我们能实现Python深度集成的核心基础,也是解锁定制化能力的关键钥匙。如果各位之前在Surpac中使用过宏功能,一定知道它的原生宏语言基于TCL,也就是工具命令语言。这门语言在多个工业领域都有广泛应用,稳定性极强。在TCL语言的基础上,我们为Surpac量身打造了专属的函数集,也就是SCL语言。基于SCL,我们开发了Guido界面系统,用于承载所有的用户交互表单。在这套体系的最上层,我们搭建了Java交互界面,Surpac所有的菜单栏、工具栏,以及内置的图表绘制功能,全部基于Java语言实现与渲染。今天我要给大家展示的,就是如何通过Python,实现Surpac原生难以完成的复杂数学运算,优化原生几何处理功能的精度短板,同时实现更专业的可视化图表输出。把这些能力组合起来,大家就能获得Surpac与Python集成的完整全景视图,真正理解这套能力能给你的日常工作带来怎样的颠覆性改变。这里我可以给大家一个非常直白的类比:TCL是Surpac原生的固定口径枪管,而Python就是我们给这把枪加装的模块化膛线与瞄准系统,精度和适配性直接提升一个量级。今天所有的演示操作,我都会在Surpac 2025 Refresh 2版本中完成。这个版本最核心的升级之一,就是支持直接拖拽Python文件进入Surpac界面,一键执行.py格式的脚本文件。二、环境配置:开启潘多拉魔盒的第一把钥匙
在正式使用这项功能之前,大家需要先完成一个非常简单的前置配置:进入软件的自定义默认偏好设置界面,就能看到专门的Python路径设置选项。这里要特别提醒大家,这个版本的Surpac支持自定义Python路径,但不会为大家自动安装Python环境。大家需要先在本地完成Python环境的安装,再在软件中指定对应的安装路径。只要完成这一步配置,后续大家就可以直接在Surpac中拖拽、执行Python脚本,无需任何额外的中间环节,整个操作流程和执行原生TCL宏完全一致。这一点对于矿业工程师来说至关重要,它意味着Python的调用门槛被降到了最低,你不需要改变自己的软件使用习惯,就能直接用上Python的全生态能力。就像我们给传统的矿用卡车加装了混动动力系统,你不需要重新学习驾驶方式,就能获得更强的动力、更低的能耗,以及更灵活的工况适配能力。三、在Surpac中执行Python的三种方式
经过我们的技术打磨,目前在Surpac中执行Python代码,主要有三种成熟可靠的实现方式,每一种都有对应的适用场景,我会在今天的分享中逐一拆解演示。第一种实现方式,是把Python代码封装在TCL脚本中,在宏执行的过程中按需调用Python代码完成对应运算,再把结果回传到Surpac原生环境中。第二种实现方式,是直接把.py格式的Python脚本文件拖拽到Surpac中,一键执行完整脚本,无需依赖TCL封装,这也是最简洁、最直接的调用方式。而直接拖拽执行的方式,又可以分为两个细分场景:一个是通过CSV等外部文件实现Surpac与Python之间的数据交互,另一个是通过Surpac 2025 Refresh 2新增的SCL命令,直接实现TCL与Python之间的变量传递。接下来的分享中,我会把这几种方式全部给大家做实战演示,首先就从最基础、也最灵活的TCL封装Python代码的方式开始讲起。这里我要先给大家明确一个核心逻辑:这几种方式没有绝对的优劣之分,只有场景适配的区别。就像露天矿用的电铲和地下矿用的铲运机,各自在对应的场景里才能发挥最大价值。四、实战案例一:基于Shapely库的多边形几何处理精度升级
我们直接进入第一个实战案例,也就是用TCL封装Python代码的落地应用。我能想到的最直观的演示场景,就是矿业工程师每天都要接触的边界线扩帮处理问题。如果我们用Surpac原生功能,对一个多边形边界执行160米的扩帮操作,软件确实能完成扩帮动作,但大家可以清晰看到,在多边形的拐角位置,实际扩帮距离远超我们设定的160米。同时在这个多边形的北端区域,扩帮后的边界完全没有贴合原始图形的几何形态,出现了非常明显的变形,这对于我们的矿山设计工作来说,是致命的精度问题。大家应该都深有体会,这个问题在爆破边界设计、征地边界划定、露天矿台阶设计等场景中,会带来非常严重的后果。爆破边界精度不足,直接会导致超挖欠挖,加剧矿石损失贫化。征地边界的几何变形,会直接导致合规风险与不必要的成本投入;台阶边界的精度误差,会影响最终的采矿方案执行效果,甚至带来边坡安全隐患。为了解决这个原生功能的痛点,我在这个Python脚本中调用了Shapely库,这是Python生态中最成熟、最强大的空间几何处理库,专门用于多边形的交并补、扩缩等运算。同样是160米的扩帮距离,用Shapely库处理后的边界,在北端区域完美贴合了原始图形的几何形态,拐角位置到扩帮后边界的垂直距离,精准控制在160米,没有任何误差。这就是Python集成给我们带来的最直接的价值:在不改变Surpac核心工作流的前提下,直接补上了原生几何算法的精度短板,让你的设计成果完全符合工程规范要求。除了扩帮操作的精度问题,Surpac原生功能在边界收缩操作中,还有一个无法解决的核心缺陷:它不支持收缩操作后生成多个独立多边形,这在很多矿山场景中是无法接受的。我们来做一个直观的测试:用原生功能对这个多边形执行60米的收缩操作,大家可以看到,除了拐角位置同样出现了距离偏差,原本应该在北部区域生成的第二个独立多边形,完全没有出现。这种情况在我们处理内排土场边界、多矿体隔离矿柱设计、采空区隔离带划定等场景中,会直接导致设计方案的缺失,影响整个采矿方案的合理性与安全性。而同样用Shapely库执行60米的收缩操作,不仅完美解决了拐角位置的距离精度问题,还在北部区域正确生成了第二个独立多边形,完全贴合原始图形的几何特征。大家可以看到,收缩后的两个多边形都有独立的线段编号,几何形态完全符合我们的工程预期,没有任何变形与精度损失,这是原生功能无论如何都无法实现的效果。这就是我们在这个场景中选择Python的核心原因:Shapely库在二维空间几何处理上的能力,已经经过了全球地理信息行业数十年的验证,成熟度与精度都远超软件原生的定制化功能。对于矿业工程师来说,你不需要自己从零开发复杂的几何算法,只需要调用Python生态中已经成熟的库,就能直接解决工作中遇到的核心痛点,这就是Python生态的真正价值。刚才我们已经看到了Python在边界处理中的实战效果,现在我们就来拆解底层的实现逻辑,看看这套流程到底是怎么运行的,让大家能完全掌握这套方法,自己动手实现对应的功能。这就是我之前提到的第一种实现方式:把Python代码完整封装在TCL宏中,通过TCL脚本驱动Python代码的执行,最终把运算结果回写到Surpac环境中。如果我们用文本编辑器打开这个宏文件,就能清晰看到它的代码结构:文件的前半部分,全部是标准的TCL代码,也就是我们熟悉的SCL命令,用于在Surpac中创建点、线等基础元素。紧接着的部分,是一个基于Guido系统开发的用户交互表单,这个表单的作用,就是让使用者可以自由输入扩帮/收缩的距离,以及生成的新多边形对应的线段编号。同时我们在代码中加入了TCL循环语句,让使用者可以连续选择多个多边形执行扩缩操作,不需要重复运行宏,大幅提升了操作的便捷性,贴合我们的日常工作习惯。从代码的第51行开始,就是Python正式介入的部分。我们先通过TCL定义一个名为Python_script的变量,然后逐行构建完整的Python脚本代码,把它全部封装在这个TCL变量中。在构建Python脚本的过程中,我们设置了一个中断节点,让用户可以在Surpac图形界面中选择需要处理的线段,弹出交互表单获取用户输入的参数,再把这些参数传入Python脚本中。接下来的代码段,会通过TCL和SCL命令,遍历用户选中线段的所有坐标点,把这些坐标数据整理成Python可以直接读取的列表格式,为后续的几何运算做好数据准备。整个流程的逻辑,就像矿山的选矿流程一样:TCL负责给矿、给入用户的操作指令与原始数据,Python负责磨矿、分选,完成核心的运算处理,最后再通过TCL把精矿产品输出回Surpac环境。当我们通过TCL完成了坐标数据的采集与整理,就可以把这些数据完整写入之前构建的Python脚本变量中,再把这个变量里的全部代码,写入一个临时的.py格式Python文件中。这一步操作完成后,我们就可以在Surpac中直接调用并运行这个Python文件了。而实现这个功能的核心,就是Surpac 2025 Refresh 2版本新增的SclPythonExecutor命令。我们只需要在这个命令中指定执行类型为file,再写入我们刚刚生成的Python文件的路径,Surpac就会自动调用本地的Python环境,完整执行这个脚本文件,不需要任何额外操作。在这个演示案例中,我们的Python文件会执行一个专门定义的多边形处理函数,把TCL传入的原始坐标数据,按照用户设定的距离完成扩缩运算,再把处理后的坐标数据输出到expanded_polygon.csv文件中。Python脚本执行完成后,我们再通过TCL代码,读取这个CSV文件中处理后的坐标数据,遍历每一行坐标信息,在Surpac中自动创建对应的点与线段,生成最终的处理后多边形。在代码的最后,我们设置了循环逻辑,只要用户没有按下退出键,就可以重复选择新的多边形执行处理,无需重新运行宏,真正实现了批量、连续的自动化操作。这套流程的核心亮点,就在于新增的SclPythonExecutor命令,它就像一座桥梁,彻底打通了Surpac原生环境与Python生态之间的壁垒,让两者可以实现无缝的数据交互与流程联动。而且这套方法的灵活性几乎没有上限,你的想象力就是唯一的边界。后续我会给大家演示,我们甚至可以通过AI生成Python脚本,再把它和TCL宏结合起来,快速实现你想要的任何定制化功能。这就像我们给Surpac搭建了一条通用的自动化生产线,你只需要更换Python脚本这个“生产模具”,就能生产出任何你需要的“产品”,无需对生产线本身做任何改造。五、实战案例二:基于Plotly的矿业数据高级可视化与报表自动化
接下来我们进入第二个实战案例,这个案例会给大家展示,如何把现有的TCL宏,和Python的可视化能力结合起来,替换掉Surpac原生的Java图表功能,实现更专业、更灵活的矿业数据可视化。我先给大家介绍一下这个原生宏的功能:它的核心作用,是对比爆破设计的孔位偏差数据,把设计孔位的标准参数,和实际施工的孔位采集数据做对比,在图形界面中直观展示偏差情况。同时,这个宏会通过Java生成对应的统计图表,最核心的就是孔位偏差区间的直方图,能清晰看到不同偏差范围内的炮孔数量,同时生成对应的统计表格,列出每个偏差区间的具体占比。除了生成图表,这个宏还会自动输出对应的CSV格式数据文件。原本这个CSV文件的作用,只是方便大家把数据复制粘贴到对应的报告中,而现在,它成了我们对接Python可视化能力的核心接口。基于这个CSV文件,我只需要给AI工具(在这个案例中我用的是Grok)输入一个非常简单的提示词,告诉它我的CSV文件的数据结构,让它用Python的Plotly库生成对应的可视化图表。我给它的需求非常明确:生成一个炮孔数量的偏差区间直方图,再生成一个炮孔偏差占比的饼图,用于爆破施工质量分析。只需要十几秒,AI就自动生成了完整可运行的Python脚本文件。生成脚本之后,我只需要把这个.py文件直接拖拽到Surpac的图形界面中,它就会自动读取宏生成的CSV数据文件,生成我需要的可视化图表,整个过程不需要任何代码修改。大家可以看到,生成的直方图按照100毫米到700毫米以上的区间,清晰展示了每个偏差范围内的炮孔数量,排序规整,数据一目了然;饼图则直观展示了不同偏差区间的占比分布。通过这个图表,我们一眼就能看到,绝大多数炮孔的偏差都控制在目标范围内,只有极少部分炮孔的偏差超出了阈值,爆破施工的质量情况一目了然,完全满足我们的技术分析与报告需求。Plotly库生成的图表,还有一个原生Java图表无法比拟的优势:它支持无损导出PNG格式图片,只需要点击下载按钮,两秒钟就能得到高清的图表图片,直接粘贴到任何报告中使用。同时,它还可以导出HTML格式的交互式图表,你可以把这个文件发给任何同事,对方不需要安装Surpac,就能打开查看交互式的图表数据,极大提升了技术沟通的效率。对于矿业工程师来说,这个功能的价值不言而喻。我们日常工作中,超过30%的时间都花在了报表制作与数据可视化上,而Python可以把这部分工作的耗时压缩到原来的十分之一。从爆破施工质量分析、矿石品位分布统计,到采矿计划执行进度跟踪、边坡监测数据展示,所有的矿业数据可视化场景,都可以通过这套方法实现自动化、标准化的输出。这就像我们给矿山的化验室配备了全自动的检测与报告生成系统,你只需要把原始数据放进去,系统就会自动完成分析、生成图表、输出报告,全程不需要人工干预,效率和精度都拉满。六、实战案例三:TCL与Python的变量双向传递与动态脚本执行
接下来我要给大家演示的第三个案例,是前面两种方法的进阶延伸,也是很多高级用户最关心的功能:如何在运行TCL宏的过程中,动态创建变量,再把这些TCL变量实时传递给Python脚本。这个方法和我之前演示的案例有本质的区别:之前的案例中,我们是在TCL宏运行时,动态生成完整的Python脚本文件再执行;而这个方法,是直接调用已经写好的Python脚本,只把TCL变量实时传递进去。我先给大家拆解这个流程的TCL部分,也就是整个流程的执行起点。首先,我们通过TCL定义一个python_file变量,里面存储的是我们要调用的、已经写好的Python脚本的文件路径。接下来,我们创建一个TCL列表,里面存储了我们要传递给Python的一组名称数据,再定义几个不同类型的TCL变量:一个存储CSV文件路径的字符串变量,一个整数变量,一个浮点型数值变量。我特意选择了这几种不同类型的变量,就是为了给大家演示,这套变量传递机制,支持所有常用的数据类型,完全能满足我们日常工作中的所有数据交互需求。在TCL代码的最后,我们还是调用SclPythonExecutor这个核心命令,指定要执行的Python脚本文件,同时把我们定义好的四个变量,作为参数传递给这个Python脚本。这里有一个非常关键的细节,大家一定要记住:我们在TCL中传递了四个变量,那么在Python脚本中,就会对应生成四个系统参数,顺序和我们传递的顺序完全一致,一一对应。现在我们切换到对应的Python脚本中,看看它是怎么接收这些变量的。脚本的第一行,我们导入了Python的sys模块,这是实现变量传递的核心,它可以接收来自命令行的所有输入参数。脚本首先会统计传入的参数数量,把所有参数打印出来,方便我们调试。按照传递的顺序,第一个传入的TCL列表,对应Python中的sys.argv[1],CSV文件路径对应sys.argv[2],整数对应sys.argv[3],浮点数对应sys.argv[4]。接下来,我们就可以在Python脚本中,对这些传入的变量做任何处理。比如对于传入的名称列表,我们用split命令把它拆分,再通过循环语句,把每个名称逐行打印出来。对于CSV文件路径,我们可以直接在Python中读取这个CSV文件,做任何数据处理与分析;对于整数和浮点型数值,我们可以直接用于数学运算,完全没有任何限制。脚本的最后,我们会输出执行完成的提示信息,告诉用户整个变量传递与处理流程已经顺利完成,整个过程清晰透明,没有任何黑箱操作。现在我们回到Surpac中,实际运行这个TCL宏,看看实际的执行效果。宏运行后,首先会把传入的名称列表拆分,逐行打印每个名称,然后依次输出CSV文件路径、整数变量、浮点型变量的数值。最后会弹出一个消息窗口,告诉我们脚本执行完成,整个流程没有任何报错,所有的TCL变量都被Python完整接收、正确处理,完美实现了我们的预期目标。这就是我们在Surpac中调用Python的第三种核心方式:先执行TCL代码获取用户输入、采集Surpac中的数据,再把这些数据作为变量,实时传递给现成的Python脚本执行。这套方法的优势在于,你可以把核心的运算逻辑封装在固定的Python脚本中,只需要通过TCL传递不同的参数,就能实现不同的功能,代码的复用性和可维护性都得到了极大的提升。就像我们矿山的固定式破碎站,Python脚本就是破碎站的主机,是固定不变的;而TCL传递的变量,就是我们给入的不同矿石,无论给入什么类型的矿石,破碎站都能按照设定的参数完成破碎作业。对于团队协作来说,这套方法的价值更是不可估量。团队里的Python高手可以写好核心的运算脚本,其他工程师只需要通过TCL宏传递参数就能使用,不需要懂Python代码,大幅降低了使用门槛。七、拓展场景:Python集成解锁的Surpac高阶能力边界
前面我们已经完整拆解了Surpac调用Python的三种核心方式,以及三个对应的核心实战场景。接下来,我给大家展示更多的拓展应用案例,帮大家打开思路,看看这套能力到底能实现多少原生功能无法完成的操作。第一个拓展案例,是地质统计学领域的应用。我们可以通过Python脚本,读取Surpac中的钻孔样品数据,执行克里金插值运算,实现样品数据的均匀化采样处理,同时直接生成对应的可视化图表。熟悉地质统计学的工程师都知道,克里金插值是资源估算的核心算法,而Surpac原生的克里金功能,虽然成熟稳定,但定制化能力有限,无法满足很多复杂矿体的个性化估算需求。而通过Python,我们可以调用Scipy、PyKrige等专业的地质统计学库,实现更灵活的变异函数拟合、更定制化的克里金插值运算,同时直接把运算结果回写到Surpac的块体模型中。这就意味着,我们可以在不脱离Surpac工作流的前提下,实现资源估算流程的全定制化,针对复杂矿体、非常规矿床的资源估算,打造专属的自动化估算流程。第二个拓展案例,是露天矿的填挖方计算与可视化。我们可以通过Python读取Surpac中的DTM数字地形模型数据,计算设计地形与原始地形之间的填挖方量,同时生成三维交互式的填挖方可视化图表。大家可以看到,这个图表中,我们清晰展示了设计平面的位置,以及不同区域的填方、挖方范围,同时搭配了对应的高程图例与填挖方量图例,还可以自由旋转、缩放图表,从任意角度查看计算结果。原生Surpac虽然也能完成填挖方计算,但无法生成这种交互式的三维可视化成果,而通过Python的Plotly库,我们只需要十几行代码,就能实现这种专业级的可视化效果。对于采矿设计工程师来说,这种交互式的填挖方图表,无论是用于方案汇报、技术评审,还是用于施工交底,都比传统的静态图纸要直观得多,能大幅提升技术沟通的效率。接下来给大家展示的,是Python可视化能力的更多进阶玩法。大家现在看到的这个图表,是基于钻孔品位数据生成的多维度统计图表,目前展示的是原始数据的线性分布统计。我们可以直接在图表上切换坐标轴的显示模式,比如从线性模式切换到对数模式,也可以通过图例,自由开启或关闭不同品位区间、不同岩性的数据序列,实现多维度的交互式数据筛选与分析。同时,这个图表同样支持一键导出高清PNG图片,也可以导出HTML交互式文件,无论是用于内部的地质数据分析,还是用于外部的资源量报告编制,都完全满足专业要求。这就是Python的Plotly可视化库给我们带来的核心优势:它的灵活性和定制化能力,是Surpac原生图表功能完全无法比拟的,你可以根据自己的需求,定制任何样式、任何维度的统计图表。从单孔的品位曲线绘制、多钻孔的对比分析,到块体模型的品位分布统计、采矿方案的经济指标对比,所有矿业数据统计场景,都可以通过Python实现自动化、定制化的可视化输出。而且对于工程师来说,你不需要自己从零编写复杂的可视化代码,现在的AI工具可以根据你的需求,快速生成对应的Python脚本,你只需要做简单的调试,就能直接在Surpac中使用。这就彻底打破了数据可视化的技术门槛,哪怕你没有任何编程基础,也能快速生成专业级的矿业数据图表,把自己从繁琐的报表制作工作中彻底解放出来。八、拓展场景:地理参考图像覆盖技术
接下来这个拓展案例,是很多地质工程师日常工作中都会遇到的需求:地理配准后的JPEG影像图在Surpac中的三维 draping(叠加贴合)处理。我们在日常工作中,经常会从QGIS、Agisoft、Propeller等软件中,获取已经完成地理配准的JPEG格式卫星影像、无人机航飞影像,这些影像文件压缩比高,文件体积很小,通常只有1-2MB。我们的核心需求,就是把这些影像图,精准贴合到Surpac的SDM数字模型或者DTM地形模型上,生成三维的实景模型,用于矿山现状查看、设计方案对比、边坡监测等场景。原生Surpac虽然也能实现影像贴合,但操作流程非常繁琐,需要手动完成控制点配准、RGF地理配准文件创建等一系列操作,稍有不慎就会出现配准偏差,耗时耗力。而通过Python的Pillow图像处理库,我们可以把这个流程完全自动化。我开发的这个宏,只需要用户选择对应的JGW配准文件、JPEG影像文件,以及要贴合的SDM模型文件,就能一键完成所有操作。Python脚本会自动读取JPEG影像的像素参数、地理配准信息,自动计算坐标对应关系,生成Surpac可识别的RGF地理配准文件,最终完成影像在三维模型上的精准贴合,全程不需要任何人工干预。大家可以看到,最终生成的三维贴合影像,坐标精准,没有任何变形,完美还原了矿山的实景情况,整个操作流程从原来的十几分钟,压缩到了十几秒钟,效率提升了上百倍。对于地质工程师、测量工程师来说,这个功能可以彻底解决影像贴合的痛点,让你可以快速把无人机航飞成果、卫星影像融入到Surpac的工作流中,大幅提升三维模型的实景化程度。九、拓展场景:EPSG代码管理与高精度变换
接下来这个拓展案例,是测量与地质工程师的又一个核心痛点:测量坐标系的高精度转换,也就是我们常说的坐标换带计算。我基于Python的PyProj库,开发了这个坐标转换的自动化宏,它的核心功能,就是实现不同坐标系、不同投影带之间的高精度坐标转换,完全覆盖我们日常工作中的所有坐标转换需求。大家可以看到,这个宏的交互表单非常简洁,你只需要选择原始坐标系和目标坐标系,就能一键完成选中线段的坐标转换。而且你可以根据自己的需求,在数据库中添加任意数量的坐标系,没有任何限制。所有的坐标系参数,都基于全球通用的EPSG编码标准,你只需要去对应的官方网站,查到目标坐标系的EPSG编码,把它录入到宏的数据库中,就能直接使用,不需要手动配置复杂的投影参数。这里要特别提醒大家,如果需要实现毫米级的高精度坐标转换,你需要下载对应的GSB格网校正文件,比如澳大利亚MGA坐标系的GSB文件,都可以直接从GitHub上免费获取,宏会自动调用这个文件完成校正。表单的底部,会实时显示你选择的原始坐标系和目标坐标系的EPSG编码,避免出现选错坐标系的低级错误,确保坐标转换的精度与准确性。原生Surpac的坐标转换功能,不仅支持的坐标系有限,而且操作流程繁琐,无法实现批量转换,更不支持高精度的格网校正。而通过Python,我们彻底解决了这些痛点,实现了全坐标系、高精度、批量的坐标转换。和之前的宏一样,这个坐标转换的自动化宏,大家如果有需要,随时可以找我索要,我会完整分享给大家,大家可以直接在工作中使用,也可以基于它做二次开发,添加更多自己需要的功能。到这里,我们今天的核心技术分享就接近尾声了,接下来我给大家总结一下今天分享的核心要点,帮大家梳理完整的知识框架,确保大家能真正掌握这套能力的核心逻辑。首先,Surpac 2025 Refresh 2版本新增的SclPythonExecutor命令,是整个Python集成能力的核心。它不仅支持在TCL宏中直接执行Python脚本,还支持TCL变量向Python脚本的实时传递。其次,我们给大家演示了第二种核心方法:通过TCL封装Python代码,在宏运行时动态生成Python脚本并执行,这种方法的灵活性最强,能适配绝大多数的定制化场景。同时,我们也给大家展示了如何通过简单的AI提示词,快速生成可用的Python代码,再把它和TCL宏结合起来,大幅降低定制化功能的开发门槛,哪怕你没有编程基础,也能快速上手。最后,我给大家展示了多个实战拓展案例,从几何处理、数据可视化,到影像贴合、坐标转换,帮大家打开了思路,看到了Python集成给Surpac带来的无限可能。希望今天的分享,能给各位带来实实在在的收获,帮大家在日常工作中,通过Python解锁Surpac的更多能力,提升工作效率,把更多的精力投入到真正创造价值的技术工作中。系列总结
作为《GEOVIA CONNECT 25X 实战系列》的收官之作,本文完整拆解了Surpac与Python深度集成的核心能力,为整个矿业全流程数字化闭环,补上了最后一块、也是最关键的一块拼图。纵观整个系列,我们以“矿业全流程价值实现”为主线,严格遵循矿山从地质勘探、资源估算、矿山规划到生产调度的真实业务流,完成了全链条的技术拆解与实战落地。在开篇的战略模块,我们确立了AI驱动矿业数字化转型的核心视野,夯实了地质统计学与资源模型的技术基础,为整个系列搭建了完整的战略与技术框架。在协作与风险管理模块,我们打破了地质与采矿部门的壁垒,解决了模型交接的核心痛点,实现了资源模型从技术文件到协作载体的价值升级,打通了地质与采矿的流程断点。在露天与地下矿山规划模块,我们完整覆盖了从战略坑优化、详细设计到生产调度的露天矿全流程,也深入拆解了地下采场优化与崩落法规划的核心技术,构建了完整的采矿设计技术体系。而作为收官之作的本文,则通过Python集成能力,把前面所有模块的工作流,都实现了极致的自动化与定制化,让每一位矿业工程师,都能打造属于自己的专属数字化工作流。对于全球矿业行业而言,数字化转型的核心,从来不是软件与工具的堆砌,而是让技术真正服务于矿山的价值创造,让工程师从重复劳动中解放出来,把精力投入到资源优化、方案创新的核心工作中。《GEOVIA CONNECT 25X 实战系列》的全部内容,都始终围绕这一核心目标。我们希望通过这十篇文章,给全球矿业工程师提供一套完整、可落地、全流程的数字化实战指南,助力行业实现真正的高质量转型。未来,我们也会持续带来GEOVIA产品体系的更多实战技术分享,持续为矿业数字化转型,提供最坚实的技术支撑与实战方法论。附录:Ash COLTON实战演讲原文(整理版)
Hi everyone, thank you for joining the Surpac session today. What I want to cover is how we can make use of Python from inside Surpac. The goal of this session will be to show you the various ways that we can do that, and there are potentially three different ways we can go about utilizing Python from inside Surpac. Before I jump into that, what I want to do is briefly touch on what we can do inside Surpac at the moment and the architecture of Surpac with its macro language.If you've utilized macros inside Surpac before, you will know that it uses TCL or Tool Command Language. This is a widely used programming language in a lot of different industries. On top of TCL, we have built our own set of functions called SCL. As part of that, we have something called Guido, which is all our forms, and on top of that, we have a Java interface where we actually use Java to display all the menus and toolbars. Also, all our charting is done with Java. I'm going to show you how we can utilize Python to do some complex mathematical operations and improve some of the geometry functions inside Surpac, and also how we can use Python to get some nice charting out as well. A combination of those things will hopefully give you a good overview of Surpac and Python.For this demonstration, I'm going to be using Surpac 2025 Refresh 2. What that allows me to do is to drag and drop Python files into Surpac and run .py files. Before I can do that, I need to come into my customized default preferences, and you'll see here that I actually have an option to set the Python path. This version of Surpac will allow you to set the Python path, but it won't actually install Python for you. As a user, you come in there, once you've installed Python, you nominate the path to where Python is, and from then on, you'll be able to drag and drop Python files.We have several ways of executing Python from inside Surpac. One of the ways that we can do it is by wrapping Python code in TCL and then executing Python when we need to. Another way that we can do it is to drag and drop a .py file directly from Surpac and execute the Python script that way. When you do that, there's a couple of different variations that we have. One of those is to have data and variables inside a CSV file or some sort of file. The other variation of that is to utilize some new SCL commands that Surpac has introduced in 2025 Refresh 2, which allow you to pass variables from TCL to Python inside a macro. I'll demonstrate both of those, and I'll start with the TCL wrapping of Python code.Let's dive into the first example, which is wrapping Python in TCL. I think one of the best ways to demonstrate this is with the problem statement of expanding a boundary. If I do this normally inside Surpac and expand this boundary string 160 meters, you can see that Surpac has expanded it. But you can also see that where the corners are, the actual distance is greater than 160 meters. That's one of the issues. At the northern end of this polygon, you will see that it hasn't quite adhered to the geometry of the shape either. One of the things that I've done with this particular Python script is use a library called Shapely, and it's very good at doing polygon intersections, outer sections, and expansions. With the same distance, 160 meters, you can see that the northern end has expanded the geometry correctly. If I run out the distance there, you can see that from the corner to the expanded boundary is exactly 160 meters. This is very useful for things like blast boundaries that are often required to be at certain distances.The other thing that Surpac does when it expands, or in this case contracts, is it doesn't have the ability to contract and form multiple polygons. If I contract this shape by 60 meters, you can see here that it's contracted it. We've got the same issue on the corners, whereby it's not adhering to the distance, but there should be a second polygon in there that's created. The Shapely library is able to contract this by 60 meters, and you'll see that it actually does form the secondary polygon up the north there. It's adhered to the geometry of those corners as well. There's 81 and 82 as my second segment there. There's several reasons for using Python in this instance, and the Shapely library is very good at doing this sort of thing.Now that we've seen Python in action to expand this boundary, let's have a look under the hood to see how it's actually being done. This is the first case that I was explaining before, whereby Python is actually wrapped in TCL and run using TCL. If I edit this one in any text editor, but I'm using ConTEXT in this case, you can see the first part of this macro here is all TCL. This is how we create new points in TCL. These are actually SCL commands. Now the next part of this is a form, a Guido form. This allows the user to enter the distance and the string number that they want the expanded polygon to be created in. We're using a TCL while loop so the user can select as many polygons as they want to expand.From line 51 onwards, you can see here this is where Python starts getting involved. I'm using a set Python script variable, which is TCL, and then what I'm doing is building a Python script line by line, but wrapping it in this TCL here. This is all going to be stored inside this one variable called Python script. About halfway through writing this Python script, I'm pausing it and allowing the user to select any segment they want to expand or contract. That will display the form, they can then choose the expansion distance and the string number they want to use. Then this bit of code here allows TCL and SCL to iterate over the points on the segment that's going to be expanded, and it puts it into a list that Python can make use of.Once we have this list, we continue writing our Python script. You can see here we're taking the variable which has got all our coordinates in it, and we're writing that into the Python script as well. Now we're writing all the values from the Python script variable into this temporary .py file. That then allows me to run my Python file direct from Surpac. This is the new part to this. We now have a command here called SclPythonExecutor. I just put in file as the type, and then I nominate the Python file that I want to run or execute, and Surpac will go away and run this. In this particular instance, this Python file is going to run what we would call a procedure or a definition, so it runs this process polygon to CSV. This expanded polygon contains all the coordinates that I've gathered using my TCL process, and it's going to put the expanded polygon into the CSV file name, which is called expanded_polygon.csv.It runs this process, outputs a CSV of the expanded polygon coordinates. I can then open that expanded polygon coordinates using TCL again. I iterate over every single line in my expanded polygon CSV and I create new points. Then it gets to the end of the while loop and if I haven't pressed escape, I can then go ahead and select another polygon for expansion. The real magic for this comes down to this new command that we've added in 2025 Refresh 2 to allow me to call these Python files direct and run them as part of a TCL macro. It's very flexible and very helpful for a lot of things that we can do. You're only really limited by your imagination here because, as you will see soon, we can use AI to create Python scripts for us, and we can incorporate those with our current TCL scripts to create something similar to this.For this next example, I want to show a pre-existing macro where I'm going to create a Python script for the charting that this macro currently does with Java. All the macro does is take design colors for a blast design and the actual pickup of those colors, and when I'm in graphics, I can compare those two. The macro spits out a Java chart. You can see that I've got a histogram here of how many holes deviate by what amount. You can see the table that's listed there. It's just listing out how many holes deviate and the actual deviation percentages themselves. That same macro also outputs a CSV file. This came in handy because the CSV file was originally meant to just paste onto another report. But what I've been able to do with the CSV file is give it over to some artificial intelligence, in this case, Grok. I've just said, Grok, this is what my CSV file is of, I'm wondering if you can create a Plotly chart for me in Python. I want a histogram for the number of holes, and I want a pie chart for the deviation percentages of those holes.It goes away and creates a .py Python file for me. I can basically drag and drop this .py file into graphics, and it utilizes the CSV file that my macro was already producing to create charts like this. I've got a hole count by deviation range, and it lists it out in nice order there, 100 millimeters to greater than 700 millimeters. It's also given me a pie chart, which is the deviation percentage distribution. You can see very quickly and easily most of my holes are within target range, and you can see a small sliver of the pie where they're outside of target range. The beauty of this with Plotly is it allows me to take screen captures or images of the actual charts. I just click on this download plot as a PNG. Two seconds later, I've got a nice chart in PNG format that I can paste onto any report that I want to as well. The HTML can be sent to people, and the PNG file can be used for reporting. It's very helpful in that regard.If I jump back in here, you can see the macro also gives me color-coded holes with the deviation listed above the hole, which is also very helpful. If you want this macro, just let me know and I'll forward it to you. This next example that I'm going to show you is a variation on what I've already demonstrated. It's where we have the use case of running a TCL program, creating some variables, and then passing those TCL variables through to a Python script at runtime. It varies from what I've shown you previously, where I actually created the Python script at runtime. This time around, I'm calling an existing Python script and just passing TCL variables to it.If I have a look at the TCL component, this is what would be executed first. I'm setting a variable called python_file to be the actual Python script. You can see it just here. I'm creating a TCL list, which is just a list of names that I'm going to pass through to Python. Here's another TCL variable, which stores this data.csv, some sort of CSV that I want Python to interact with, an integer value, and a real value, just to show you some of the differences. Down at the bottom, I am using the new SclPythonExecutor command, and it's going to execute the Python file, and these are the variables that I'm going to pass through to it. Take note of this: I've got four variables that I'm passing through to my Python script.If I click across to my Python script and have a look at that, we can see that straight away it's importing the sys module for it to work. It's going to count the number of arguments that are actually coming through. It prints those system arguments out for me. The first argument was my list of names, so that comes through here in argument one. The CSV file was in argument two, the integer value was argument three, and the real value was stored in argument four. Then it just uses some Python code to print the names. It's now stored in this names variable. The Python string names comes in there, using a split command to split them out properly and then looping over them one by one, which you can see. We print out the name of the CSV file here, though we're not doing anything with it. We print out the integer value and print out the real value. Then it's just saying that it is complete.Let's have a look at that and how that would run. I jump back into Surpac. Remember that I'm running my TCL code. That has passed all those variables through to Python. You can see that's the first list that gets called. It splits it so you can see each name line by line. It's printing out what value was stored in argument two, argument three was the integer value, and argument four was the real value. Then it's just printing a message window saying it's complete. This is another way that we can utilize the new command to execute Python code. We're executing the TCL first and passing those TCL variables through to the Python code at runtime.To finish off, I thought we'd have a look at some examples just to spark the imagination of what can be done. Here is one that takes some data and performs kriging on it to essentially give you uniform sampling throughout and charts that up there. That's just another charting type of Python script. Here is one that demonstrates some cut and fill. I have a plane here and some areas of cut and areas of fill, an elevation legend over here, and a cut and fill legend here as well. You can rotate the charts around. Again, this is another Plotly chart. I've got another example here of some charting. It's at raw at the moment. Let's try log, let's try linear. We can interact with the legend over here, turn things on and off. We've got the option there to download as a PNG. We have really flexible options with Plotly from the Python library.A couple of other options or examples that we have include draping georeferenced JPEG files. This is quite common when you get JPEG files from packages like QGIS, Agisoft, or Propeller. The JPEGs are good because they're highly compressed and actually very small. This one here is 1.1 megabytes. The SDM that I want to drape it on is not that big but reasonably detailed. The macro will create an RGF file for me. If you're familiar with Surpac, the RGF file is what links the JPEG to the SDM or DTM. You can drag it in at any time and see the draped image. I'll select the JGW file, the JPEG, and the SDM. This Pillow library is where the magic happens. It will investigate the JPEG, work out the pixel sizes, and then create the RGF file and do the draping for me. We end up with a draped three-dimensional image. That saves a lot of hassle about registering points in graphics.Another thing we can do here is string transformations. I'll show you the form, it looks like this. It allows me to choose a grid system that I want to go from and to. I can add as many of these grid systems as I like. All I need to do is go to this website, look up the EPSG code, and put it into the database of this macro to allow me to do the transformations. If you want the high precision transformation, you need to have the associated GSB file, which, for the case of MGA, can be downloaded from GitHub. Down at the bottom is just a label to tell you the EPSG code you're going from and what you're converting it to. This is another macro that you're welcome to have, just let me know.In conclusion, I'll summarize the main points. Essentially, Surpac 2025 Refresh 2 will have a new command called SclPythonExecutor. That's going to allow you to both execute Python scripts from inside TCL macros and pass TCL variables through to Python scripts as well. On top of that, we also saw how we can use a TCL wrapper to wrap our Python code at runtime. That's another way of passing TCL variables through to Python scripts, a very effective method. We also saw how we could generate Python code using some simple AI prompts and adding those to our TCL macros as well. I hope you enjoyed the presentation and got something out of it. Please feel free to ask me some questions, I'm going to be hanging around for question time.洞察3D UNIV+RSES,洞察生成式经济,洞察体验经济,洞察循环经济,洞察可持续发展,洞察工业复兴,欢迎订阅《升华洞察》公众号!如有合作意向,请加主编微信号“pearapple”留言。