如何在Freeplane中实现MapProxy与MindMap的转换?
解决Freeplane Groovy脚本中MapProxy到MindMap的类型转换问题
核心问题
通过c.mapLoader().load()加载本地.mm文件得到的是MapProxy类型,但c.export()方法要求传入org.freeplane.api.MindMap类型参数,直接转换或手动构建上下文的方式存在类型适配问题。
正确解决方案
使用Freeplane提供的ProxyFactory工具类,将MapProxy的底层模型对象转换为符合API要求的MindMap实例,无需手动构建复杂的脚本上下文。
完整可运行脚本:
import org.freeplane.plugin.script.proxy.MapProxy import org.freeplane.plugin.script.proxy.ProxyFactory import static groovy.io.FileType.FILES import org.freeplane.api.MindMap boolean overwriteExistingFile = true String directory = 'data' new File(directory).eachFileRecurse(FILES) { file -> if (file.name.endsWith('.mm')) { // 加载本地.mm文件得到MapProxy MapProxy mapProxy = c.mapLoader(file.path).load() // 通过ProxyFactory生成符合API要求的MindMap实例 MindMap mindMap = ProxyFactory.createMindMap(mapProxy.delegate, c.scriptContext) // 生成输出SVG路径 String outputPath = "/tmp/${file.name.replaceFirst('.mm$', '.svg')}" // 调用导出方法 c.export(mindMap, outputPath, 'Scalable Vector Graphic (SVG) (.svg)', overwriteExistingFile) } }
错误尝试分析
- 直接获取底层模型对象:
你之前尝试的theMap.delegate.getRootNode().map获取的是Freeplane内部的MindMapModel对象,并非API层的MindMap接口实现,因此无法被c.export()识别。 - 手动构建ScriptContext:
手动创建NodeScript和ScriptContext属于冗余操作,脚本运行时的上下文c.scriptContext已经可以直接使用,无需重新构建。
内容的提问来源于stack exchange,提问作者Interlated
相关产品推荐
相关产品推荐

